Fast and clever way to get the NON FIRST segment of a C# string

前端 未结 6 2206
太阳男子
太阳男子 2021-02-18 21:22

I do a split(\' \') over an string and I want to pull the first element of the returned string in order to get the rest of the string.

f.e. \"THIS IS

相关标签:
6条回答
  • 2021-02-18 21:50

    Try this

      string str = "THIS IS AN AMAZING STRING";
      string firstString = str.Split(' ')[0];  //get the first string
      string newStr = str.Replace(firstString + " ", "");  //remove the first string
    
      //OR
    
      string newStr = str.Remove(0, firstString.Length + 1);
    
    0 讨论(0)
  • 2021-02-18 21:58

    try

    string X = "THIS IS AN AMAZING STRING";
    string Y = (X.IndexOf ( " " ) < 0) ? string.Empty : X.Substring (X.IndexOf ( " " )  + 1); // Y = IS AN AMAZING STRING
    

    As per comment (IF X is guaranteed to be a valid string with at least one space) a simpler version without checking etc.:

    string Y = X.Substring (X.IndexOf ( " " )  + 1); 
    
    0 讨论(0)
  • 2021-02-18 21:58

    Very simply, you could do:

    string input = "THIS IS AN AMAZING STRING";
    string result = input.Substring(input.IndexOf(' ') + 1);
    

    This will work for the type of input you're specifying (where there is always a space between the first two words in your string), but for case where the input string does not have spaces at all, it returns the whole string as-is.

    0 讨论(0)
  • 2021-02-18 22:01
    var a = "THIS IS AN AMAZING STRING".Split(' ');
    string amazing = String.Join(" ", a.Skip(1));
    

    If you are on pre-.NET 4, you need to stick a .ToArray() in the end of the Skip call - because the String.Join overload that takes enumerable as the second parameter was first added in .NET 4.

    While this works well in the general case, if you always want to remove just the first word, there is a better way of doing this, as pointed out by Reed in comments:

    var a = "THIS IS AN AMAZING STRING".Split(new char[] {' ' }, 2);
    string amazing = a[1];  //Perhaps do a length check first if you are not sure there is a space in the original string.
    

    This performs better for larger strings, since Split only need to look until it finds the first space, and can then create a result with two strings only - and it avoids the String.Join which could also be expensive, especially for longer strings.

    0 讨论(0)
  • 2021-02-18 22:07

    Do NOT use the Split function - a) it looks for every ' ' symbol there is, not just the first one. b) this approach will involve lots of copying data around in memory which is rather slow operation for strings.

    var a = "THIS IS AN AMAZING STRING";
    string result;
    var index = a.IndexOf(' ');
    if (index == -1)
        result = null;
    else
        result = a.Substring(index + 1);
    

    Since the title of the question mentions array, not string, it is worth mentioning the ArraySegment class - this enables you to create a pointer to part of the array (without creating a new array and copying data):

    var a = new int[] { 0, 1, 2, 3 };
    var segment = new ArraySegment<int>(a, 1, a.Length - 1);
    
    0 讨论(0)
  • 2021-02-18 22:14

    A fairly good option would be to use:

    string original = "THIS IS AN AMAZING STRING";
    string[] split = original.Split(new []{' '}, 2);
    string result = split[1];
    

    Note that, if you just want the resulting string, you can shorten this:

    var result = original.Split(new []{' '}, 2)[1];
    

    By using the overload of string.Split which takes a max number of splits, you avoid the need to join as well as the extra overhead.

    0 讨论(0)
提交回复
热议问题