c# string operation
-
The simplest solution is to use a regular expression:
Regex.Replace(inputString, @"[\s]*$", "")
All those who believe in psycho kinesis, raise my hand.
-
you can do input_3 = inputstr.Substring(0, inputstr.LastIndexOf(" ")); will give you the result you want.
theLizard
Unfortunately, that turns out not to be the case. It will remove all data from the last occurance of a space to the end: "A<space>B<space>C<space><space>" becomes "A<space>B<space>C<space>" "A<space>B<space>C" becomes "A<space>B"
All those who believe in psycho kinesis, raise my hand.
-
Hi All, say i have these inputs strings: string input_1 = " ABCD "; string input_2 = " A BC D "; string input_3 " A B C D "; I want to convert these strings to the follows: input_1 = " ABCD"; input_2 = " A BC D"; input_3 " A B C D"; basically remove all the blanks after the last letter, i know the string.TrimEnd() function in .Net can be used to achieve my requirement. Is there another way (without using string.TrimEnd() function) to achieve the same as String.TrimEnd() does? Many thanks
There are plenty of ways of doing this. Here is one: You can make use of
LastIndexOf
andReplace
method along with theLength
property. Check the last index of the space and if it is equal to the length of the string, replace it with nothing. You can also use the Remove method instead of Replace.50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
-
There are plenty of ways of doing this. Here is one: You can make use of
LastIndexOf
andReplace
method along with theLength
property. Check the last index of the space and if it is equal to the length of the string, replace it with nothing. You can also use the Remove method instead of Replace.50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
Doesn't achieve what he asked for: it only removes the last space, not all trailing spaces.
All those who believe in psycho kinesis, raise my hand.
-
Doesn't achieve what he asked for: it only removes the last space, not all trailing spaces.
All those who believe in psycho kinesis, raise my hand.
-
Do that recursively. :) /This approach can win the worst approach award. //No, I can beat this one too.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
Mmmm! I love the smell of sledgehammer in the morning! :laugh:
All those who believe in psycho kinesis, raise my hand.
-
Mmmm! I love the smell of sledgehammer in the morning! :laugh:
All those who believe in psycho kinesis, raise my hand.
-
Unfortunately, that turns out not to be the case. It will remove all data from the last occurance of a space to the end: "A<space>B<space>C<space><space>" becomes "A<space>B<space>C<space>" "A<space>B<space>C" becomes "A<space>B"
All those who believe in psycho kinesis, raise my hand.
Oops, less haste, If I could not use trim() it should have been something like
if(string.LastIndexOf(" ") = string.Length) { string = string.Substring(0, string.Length-1); }
But the answer from OriginalGriff seems to be the one, I have also learned.theLizard
-
Oops, less haste, If I could not use trim() it should have been something like
if(string.LastIndexOf(" ") = string.Length) { string = string.Substring(0, string.Length-1); }
But the answer from OriginalGriff seems to be the one, I have also learned.theLizard
Make it a
while
loop and use a comparison operator rather than assignment. :-D -
Make it a
while
loop and use a comparison operator rather than assignment. :-Doops (again, bugger) :( left out one of these = and yes a while would be appropriate if more than one at end of string. I'm having an off day
theLizard