我想逐字逐句地遍历字符串.
如果我有一个字符串"incidentno和fintype或unitno",我想逐一阅读每个单词"incidentno","and","fintype","or"和"unitno".
Guf*_*ffa 16
foreach (string word in "incidentno and fintype or unitno".Split(' ')) {
...
}
Run Code Online (Sandbox Code Playgroud)
Dar*_*rov 13
var regex = new Regex(@"\b[\s,\.-:;]*");
var phrase = "incidentno and fintype or unitno";
var words = regex.Split(phrase).Where(x => !string.IsNullOrEmpty(x));
Run Code Online (Sandbox Code Playgroud)
即使.,; tabs and new lines
你的单词之间有" ",这也有效.
JDu*_*ley 11
稍微扭曲我知道,但您可以将迭代器块定义为字符串上的扩展方法.例如
/// <summary>
/// Sweep over text
/// </summary>
/// <param name="Text"></param>
/// <returns></returns>
public static IEnumerable<string> WordList(this string Text)
{
int cIndex = 0;
int nIndex;
while ((nIndex = Text.IndexOf(' ', cIndex + 1)) != -1)
{
int sIndex = (cIndex == 0 ? 0 : cIndex + 1);
yield return Text.Substring(sIndex, nIndex - sIndex);
cIndex = nIndex;
}
yield return Text.Substring(cIndex + 1);
}
foreach (string word in "incidentno and fintype or unitno".WordList())
System.Console.WriteLine("'" + word + "'");
Run Code Online (Sandbox Code Playgroud)
其优点是不为长字符串创建大数组.
使用字符串类的Split方法
string[] words = "incidentno and fintype or unitno".Split(" ");
Run Code Online (Sandbox Code Playgroud)
这将在空格上拆分,因此“单词”将具有[incidentno,and,fintype,or,unitno]
.
归档时间: |
|
查看次数: |
16808 次 |
最近记录: |