none
More efficient way to remove 1st character from a string RRS feed

  • Question

  • Hi..

    if a string starts with ; then i want to remove first character(;) from the string

    can any one help in more efficient way

    Ex:    ;xyz  then i need to remove ; 

    Thursday, June 6, 2013 2:52 PM

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!

    Thursday, June 6, 2013 3:10 PM

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!

    Thursday, June 6, 2013 3:10 PM
  • 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


    Thursday, June 6, 2013 4:04 PM
  • Hi,

    You need to apply this code:

    string original = ";xyz";
    if( original.StartsWith( ";" ) )
    {
        original = original.Remove(0,1);
    }

    João Sousa (MCTS) Senior Software Engineer

    Thursday, June 6, 2013 9:35 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

    Friday, June 7, 2013 2:14 AM
  • 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.
    Friday, June 7, 2013 11:57 AM
  • 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

    Friday, June 7, 2013 2:47 PM
  • 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

    Saturday, June 8, 2013 8:14 AM