作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我想做的是获取一个字符串并返回所有可能的长度大于 2 的子字符串。因此使用 welcome
示例:
we
el
lc
co
me
wel
elc
lco
com
ome
welc
elco
lcom
come
and so on.....
我能想到的唯一方法是这样的(完全未经测试):
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));
}
}
但我想知道是否有我不知道的更好的方法(可能使用 LINQ)?
最佳答案
这种简单易读的方法怎么样?
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);
它产生:
we
wel
welc
welco
welcom
welcome
el
elc
elco
elcom
elcome
lc
lco
lcom
lcome
co
com
come
om
ome
me
关于c# - 如何找到字符串中所有可能的子串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10712903/
我正在尝试创建一个程序,其中字符串的前三个字符重复给定次数,如下所示: foo('Chocolate', 3) # => 'ChoChoCho' foo('Abc', 3) # => 'AbcAbcA
我有以下字符串: std::string str = "Mode:AAA:val:101:id:A1"; 我想分离一个位于 "val:" 和 ":id" 之间的子字符串,这是我的方法: std::st
DNA 字符串可以是任意长度,包含 5 个字母(A、T、G、C、N)的任意组合。 压缩包含 5 个字母(A、T、G、C、N)的 DNA 字母串的有效方法是什么?不是考虑每个字母表 3 位,我们可以使用
是否有一种使用 levenstein 距离将一个特定字符串与第二个较长字符串中的任何区域进行匹配的好方法? 例子: str1='aaaaa' str2='bbbbbbaabaabbbb' if str
使用 OAuth 并使用以下函数使用我们称为“foo”(实际上是 OAuth token )的字符串加密 key public function encrypt( $text ) { // a
我是一名优秀的程序员,十分优秀!