Answered by:
More efficient way to remove 1st character from a string

Question
-
Answers
-
The basic technique:
string original = ";xyz"; if( original.StartsWith( ";" ) ) { original = original.Subtring( 1 ); }
Or if you like ternary one-liners:
string filtered = (original.StartsWith( ";" )) ? original.Substring( 1 ) : original;
Or if you want to remove multiple ;'s and you like LINQ then do this:
string filtered = new string( original.SkipWhile( c=>c==';' ).ToArray() );
Or you can use a regular expression.
string filtered = Regex.Match(original,@"^\;?(.*)$").Groups[1].Value;
There are so many ways!
- Proposed as answer by Fernando Soto - MCSD Thursday, June 6, 2013 3:27 PM
- Marked as answer by Avatar 123 Monday, June 10, 2013 10:04 AM
All replies
-
The basic technique:
string original = ";xyz"; if( original.StartsWith( ";" ) ) { original = original.Subtring( 1 ); }
Or if you like ternary one-liners:
string filtered = (original.StartsWith( ";" )) ? original.Substring( 1 ) : original;
Or if you want to remove multiple ;'s and you like LINQ then do this:
string filtered = new string( original.SkipWhile( c=>c==';' ).ToArray() );
Or you can use a regular expression.
string filtered = Regex.Match(original,@"^\;?(.*)$").Groups[1].Value;
There are so many ways!
- Proposed as answer by Fernando Soto - MCSD Thursday, June 6, 2013 3:27 PM
- Marked as answer by Avatar 123 Monday, June 10, 2013 10:04 AM
-
if ( ! string.IsNullOrEmpty(yourString) && yourString[0] == ';') yourString = yourString.Remove(0, 1);
Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
Instant C# - VB to C# Converter
Instant VB - C# to VB Converter- Edited by Dave Doknjas Thursday, June 6, 2013 4:23 PM
-
-
"can any one help in more efficient way"
More efficient than what? You haven't shown any code for us to improve on.
But ... you cannot remove the first character from a string! Strings are immutable which is a fancy way of saying they cannot be changed. What you can do is create a new string which is the same as the first one from the second position on. You can then assign that brand new string to the old variable, you now have two strings the original with the ';' and the new one without.
string x = ";xyz"; string y = x; if (x[0] == ';') x = x.Substring(1); Console.WriteLine(x); \\ xyz Console.WriteLine(y); \\ ;xyz
Paul Linton
-
-
The only thing I don't like about just starting by comparing x[0] it that is causes an exception if x is the empty string, which I'm assuming might be an expected case.
That's why you should first check string.IsNullOrEmpty.Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
Instant C# - VB to C# Converter
Instant VB - C# to VB Converter -
Hi Wyck, Dave
I was trying to make 2 points
a) You cannot remove a character from a string. I mentioned this because it is a common confusion.
b) If you ask for code that is "more efficient" then you have to provide the benchmark that is required to be improved on. Something cannot be "more efficient" in isolation.
My code was all about being an explanation of how you can only give the appearance of removing a character from a string. That is why the variable 'y' was included. There was never any intention of trying to 'solve' the problem because I did not (and still do not) know what the problem was! Until we are told what the code is that we are trying to be more efficient than, no one can solve the original problem.
Paul Linton