Abe*_*ler 10 .net c# linq string
我想要做的是获取一个字符串并返回大于长度2的所有可能的子串.所以使用welcome示例:
we
el
lc
co
me
wel
elc
lco
com
ome
welc
elco
lcom
come
and so on.....
Run Code Online (Sandbox Code Playgroud)
我能想到的唯一方法就是这样(完全未经测试):
for (int i = 0; i < word.Length; i++) //i is starting position
{
for (int j = 2; j + i < word.Length; j++) //j is number of characters to get
{
wordList.Add(word.SubString(i, j));
}
}
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有更好的方法来做到这一点(可能使用LINQ),我不知道?
Eni*_*ity 13
这对于一个简单易读的方法怎么样?
var text = "welcome";
var query =
from i in Enumerable.Range(0, text.Length)
from j in Enumerable.Range(0, text.Length - i + 1)
where j >= 2
select text.Substring(i, j);
Run Code Online (Sandbox Code Playgroud)
它产生:
we
wel
welc
welco
welcom
welcome
el
elc
elco
elcom
elcome
lc
lco
lcom
lcome
co
com
come
om
ome
me
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8978 次 |
| 最近记录: |