[英]C# How to get Words From String
Hi guys i was trying a lot to retrieve some other strings from one main string. 嗨,大家好,我很想从一个主字符串中检索其他字符串。
string src = "A~B~C~D";
How can i retrieve separately the letters? 如何分别找回字母? Like: 喜欢:
string a = "A";
string b = "B";
string c = "C";
string d = "D";
you could use Split(char c)
that will give you back an array of sub string
s seperated by the ~ symbol. 您可以使用Split(char c)
来返回由〜符号分隔的子string
数组。
string src = "A~B~C~D";
string [] splits = src.Split('~');
obviously though, unless you know the length of your string/words in advance you won't be able to arbitrarily put them in their own variables. 但是显然,除非您事先知道字符串/单词的长度,否则您将无法随意将它们放入自己的变量中。 but if you knew it was always 4 words, you could then do 但是如果您知道它总是4个字,那么您可以
string a = splits[0];
string b = splits[1];
string c = splits[2];
string d = splits[3];
Please try this 请尝试这个
string src = "A~B~C~D"
//
// Split string on spaces.
// ... This will separate all the words.
//
string[] words = src .Split('~');
foreach (string word in words)
{
Console.WriteLine(word);
}
Try this one. 试试这个。 It will split your string with all non-alphanumeric characters. 它将使用所有非字母数字字符拆分字符串。
string s = "A~B~C~D";
string[] strings = Regex.Split(s, @"\W|_");
You can do: 你可以做:
string src = "A~B~C~D";
string[] strArray = src.Split('~');
string a = strArray[0];
string b = strArray[1];
string c = strArray[2];
string d = strArray[3];
string src = "A~B~C~D";
string[] letters = src.Split('~');
foreach (string s in letters)
{
//loop through array here...
}
Consider... 考虑...
string src = "A~B~C~D";
string[] srcList = src.Split(new char[] { '~' });
string a = srcList[0];
string b = srcList[1];
string c = srcList[2];
string d = srcList[3];
string words[]=Regex.Matches(input,@"\w+")
.Cast<Match>()
.Select(x=>x.Value)
.ToArray();
\\w
matches a single word ie AZ or az or _
or a digit \\w
匹配一个单词,即AZ或az或_
或一个数字
+
is a quantifier which matches preceding char 1 to many times +
是一个将前一个char 1匹配多次的量词
I like to do it this way: 我喜欢这样:
List<char> c_list = new List<char>();
string src = "A~B~C~D";
char [] c = src.ToCharArray();
foreach (char cc in c)
{
if (cc != '~')
c_list.Add(cc);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.