2012-11-13 13 views
13

मैं स्ट्रिंग के पहले 250 शब्द कैसे प्राप्त करूं?एक स्ट्रिंग के पहले 250 शब्द प्राप्त करें?

+6

'str.Split()। (250)' –

+0

[आपने क्या प्रयास किया है?] (Http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect- question.aspx) – MyCodeSucks

+1

रिक्त स्थान – malinois

उत्तर

27

आपको स्ट्रिंग को विभाजित करने की आवश्यकता है। आप पैरामीटर के बिना overload का उपयोग कर सकते हैं (व्हाइटस्पेस ग्रहण किए जाते हैं)।

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

ध्यान दें कि आप Enumerable.Take के लिए using System.Linq जोड़ने की जरूरत है।

आप ToList() उपयोग कर सकते हैं या ToArray() ro क्वेरी से एक नया संग्रह बनाने या स्मृति को बचाने और इसे सीधे गणना:

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

अद्यतन

चूंकि यह काफी लोकप्रिय हो रहा है मैं निम्नलिखित एक्सटेंशन जोड़ रहा हूं जो Enumerable.Take दृष्टिकोण से अधिक कुशल है और इसके बजाय संग्रह (स्थगित निष्पादित) क्वेरी

यह String.Split का उपयोग करता है जहां white-space characters को विभाजक माना जाता है यदि विभाजक पैरामीटर शून्य है या इसमें कोई वर्ण नहीं है। लेकिन विधि भी अलग सीमांकक पारित करने के लिए अनुमति देता है:

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; 
} 

यह आसानी से इस्तेमाल किया जा सकता: टिम जवाब देने के लिए

string str = "A B C D E F"; 
string[] words = str.GetWords(5, null, StringSplitOptions.RemoveEmptyEntries); // A,B,C,D,E 
+1

"अगर विभाजक पैरामीटर शून्य है या इसमें कोई वर्ण नहीं है, तो सफेद-स्पेस वर्णों को डिलीमीटर माना जाता है।" –

+0

+1 ... यदि प्रश्न वास्तविक होगा तो स्पेस/व्हाइटस्पेस/सेपरेटर्स के लिए कुछ खोज अधिक उपयुक्त होंगे (जैसे "\ w + \ s"), संभावित रूप से 'उपज रिटर्न' के साथ मिलकर पहले 250 शब्दों को पढ़ने के लिए नहीं। –

+0

@AlexeiLevenkov: ध्यान दें कि तर्क के बिना 'स्प्लिट' 'स्प्लिट ('') 'जैसा नहीं है। यह ['char.IsWhiteSpace'] (http://msdn.microsoft.com/en-us/library/t809ektx.aspx) आंतरिक रूप से उपयोग करता है जिसमें एकाधिक वर्ण शामिल हैं। –

9
yourString.Split(' ').Take(250); 

मुझे लगता है। आपको अधिक जानकारी प्रदान करनी चाहिए।

+8

250 लेने से पहले आप सरणी से एक नई सूची क्यों बनाते हैं? –

+0

मैं आईन्यूमेरेबल के आलसी मूल्यांकन – BlackBear

+0

@TimSchmelter स्मृति से टिपिंग का लाभ उठाने के लिए ToList से पहले लेना चाहता हूं। मुझे यकीन नहीं था कि क्या ऐरे IENumerable था। यह कुछ सी # संस्करण पहले नहीं था। संपादन। धन्यवाद। – LMB

1
string testString = "The quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dog." 
string firstWords = Regex.Match(testString, @"^(\w+\b.*?){250}").ToString(); 
0

अलावा, आप यही कोशिश कर सकते हैं है

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()); 
4

स्ट्रिंग.जॉइन ("", yourstring.Split()। ले लें (250)। टोएरे())

0

इसे आजमाएं:

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

टेक() को कॉल किए बिना यह संभव है।

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

कौन सा भी सिर्फ स्थान की तुलना में "" अधिक के आधार पर बंटवारे की अनुमति देता है और इसलिए घटनाओं को ठीक करता है जब रिक्त स्थान को गलत तरीके से इनपुट में याद कर रहे हैं।

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

StringSplitOptions.None का उपयोग करें, यदि आप इसके बजाय खाली तार वापस लौटना चाहते हैं।

संबंधित मुद्दे