Extract the last word from a string using C#

前端 未结 7 1715
醉梦人生
醉梦人生 2020-12-15 16:37

My string is like this:

string input = \"STRIP, HR 3/16 X 1 1/2 X 1 5/8 + API\";

Here actually I want to extract the last word, \'API\', an

相关标签:
7条回答
  • 2020-12-15 16:59

    First:

    using System.Linq; // System.Core.dll
    

    then

    string last = input.Split(' ').LastOrDefault();
    
    // or
    
    string last = input.Trim().Split(' ').LastOrDefault();
    
    // or
    
    string last = input.Trim().Split(' ').LastOrDefault().Trim();
    
    0 讨论(0)
  • 2020-12-15 17:06
    var lastWord = input.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries).Last();
    
    0 讨论(0)
  • 2020-12-15 17:10

    var last = input.Substring(input.LastIndexOf(' ')).TrimStart();

    This method doesn't allocate an entire array of strings as the others do.

    0 讨论(0)
  • 2020-12-15 17:12

    Well, the naive implementation to that would be to simply split on each space and take the last element.

    Splitting is done using an instance method on the String object, and the last of the elements can either be retrieved using array indexing, or using the Last LINQ operator.

    End result:

    string lastWord = input.Split(' ').Last();
    

    If you don't have LINQ, I would do it in two operations:

    string[] parts = input.Split(' ');
    string lastWord = parts[parts.Length - 1];
    

    While this would work for this string, it might not work for a slightly different string, so either you'll have to figure out how to change the code accordingly, or post all the rules.

    string input = ".... ,API";
    

    Here, the comma would be part of the "word".

    Also, if the first method of obtaining the word is correct, that is, everything after the last space, and your string adheres to the following rules:

    • Will always contain at least one space
    • Does not end with one or more spaces (in case of this you can trim it)

    Then you can use this code that will allocate fewer objects on the heap for GC to worry about later:

    string lastWord = input.Substring(input.LastIndexOf(' ') + 1);
    

    However, if you need to consider commas, semicolons, and whatnot, the first method using splitting is the best; there are fewer things to keep track of.

    0 讨论(0)
  • 2020-12-15 17:14
    static class Extensions
    {
        private static readonly char[] DefaultDelimeters = new char[]{' ', '.'};
    
        public string LastWord(this string StringValue)
        {
            return LastWord(StringValue, DefaultDelimeters);
        }
    
        public string LastWord(this string StringValue, char[] Delimeters)
        {
            int index = StringValue.LastIndexOfAny(Delimeters);
    
            if(index>-1)
                return StringValue.Substring(index);
            else
                return null;
        }
    }
    
    class Application
    {
        public void DoWork()
        {
            string sentence = "STRIP, HR 3/16 X 1 1/2 X 1 5/8 + API";
            string lastWord = sentence.LastWord();
        }
    }
    
    0 讨论(0)
  • 2020-12-15 17:18
    string input = "STRIP, HR 3/16 X 1 1/2 X 1 5/8 + API";
    var a = input.Split(' ');
    Console.WriteLine(a[a.Length-1]);
    
    0 讨论(0)
提交回复
热议问题