Get first 250 words of a string?

前端 未结 6 538
梦如初夏
梦如初夏 2020-12-11 01:51

How do I get the first 250 words of a string?

相关标签:
6条回答
  • 2020-12-11 02:26
    yourString.Split(' ').Take(250);
    

    I guess. You should provide more info.

    0 讨论(0)
  • 2020-12-11 02:43

    Addition to Tim answer, this is what you can try

    IEnumerable<string> words = str.Split().Take(250);
    StringBuilder firstwords = new StringBuilder();
    foreach(string s in words)
    {
       firstwords.Append(s + " ");
    }
    firstwords.Append("...");
    Console.WriteLine(firstwords.ToString());
    
    0 讨论(0)
  • 2020-12-11 02:44

    String.Join(" ", yourstring.Split().Take(250).ToArray())

    0 讨论(0)
  • 2020-12-11 02:44

    Try this one:

    public string TakeWords(string str,int wordCount)
    {
        char lastChar='\0';
        int spaceFound=0;
        var strLen= str.Length;
        int i=0;
        for(; i<strLen; i++)
        {
            if(str[i]==' ' && lastChar!=' ')
            {
                spaceFound++;
            }
            lastChar=str[i];
            if(spaceFound==wordCount)
                break;
        }
        return str.Substring(0,i);
    }
    
    0 讨论(0)
  • 2020-12-11 02:46

    It's possible without calling Take().

    string[] separatedWords = stringToProcess.Split(new char[] {' '}, 250, StringSplitOptions.RemoveEmptyEntries);
    

    Which also allows splitting based on more than just space " " and therefore fixes occurrences when spaces are incorrectly missing in input.

    string[] separatedWords = stringToProcess.Split(new char[] {' ', '.', ',' ':', ';'}, 250, StringSplitOptions.RemoveEmptyEntries);
    

    Use StringSplitOptions.None, if you want empty strings to be returned instead.

    0 讨论(0)
  • 2020-12-11 02:47

    You need to split the string. You can use the overload without parameter(whitespaces are assumed).

    IEnumerable<string> words = str.Split().Take(250);
    

    Note that you need to add using System.Linq for Enumerable.Take.

    You can use ToList() or ToArray() ro create a new collection from the query or save memory and enumerate it directly:

    foreach(string word in words)
        Console.WriteLine(word);
    

    Update

    Since it seems to be quite popular I'm adding following extension which is more efficient than the Enumerable.Take approach and also returns a collection instead of the (deferred executed) query.

    It uses String.Split where white-space characters are assumed to be the delimiters if the separator parameter is null or contains no characters. But the method also allows to pass different delimiters:

    public static string[] GetWords(
           this string input,
           int count = -1,
           string[] wordDelimiter = null,
           StringSplitOptions options = StringSplitOptions.None)
    {
        if (string.IsNullOrEmpty(input)) return new string[] { };
    
        if(count < 0)
            return input.Split(wordDelimiter, options);
    
        string[] words = input.Split(wordDelimiter, count + 1, options);
        if (words.Length <= count)
            return words;   // not so many words found
    
        // remove last "word" since that contains the rest of the string
        Array.Resize(ref words, words.Length - 1);
    
        return words;
    }
    

    It can be used easily:

    string str = "A B C   D E F";
    string[] words = str.GetWords(5, null, StringSplitOptions.RemoveEmptyEntries); // A,B,C,D,E
    
    0 讨论(0)
提交回复
热议问题