Suppose I have the string str = "слово1^слово2^слово3^...^словоN" . How do I get the "word1"?
You can use str.Split('^')[0] , but it seems to me that for large N this is not entirely rational, I do not need other "words".
Suppose I have the string str = "слово1^слово2^слово3^...^словоN" . How do I get the "word1"?
You can use str.Split('^')[0] , but it seems to me that for large N this is not entirely rational, I do not need other "words".
The string.Split method has an overload that accepts the maximum number of substrings to return:
var str = "слово1^слово2^слово3^...^словоN"; var words = str.Split(new char[] { '^' }, 1); Console.WriteLine(words.Length); // 1 This is effective in the case of large N.
I will clarify.
If you need to get one first value, I would take a method from another answer with Substring + IndexOf .
If you need to get the first few values, I would take the Split method with the count parameter.
Maybe then so?
str = "слово1^слово2..."; String word = str.Substring(0, str.IndexOf('^')); We look, where for the first time the delimiter occurs, choose a substring ...
Source: https://ru.stackoverflow.com/questions/792921/
All Articles