C# 如何从c中的字符串中获取子字符串?
我有一个大字符串,它存储在一个字符串变量str中。我想从c中得到一个子字符串? 假设字符串为:从此实例检索子字符串。子字符串从指定的字符位置开始 子字符串结果我想显示的是:子字符串从指定的字符位置开始 将为您提供从位置0到位置9的前10个字符C# 如何从c中的字符串中获取子字符串?,c#,C#,我有一个大字符串,它存储在一个字符串变量str中。我想从c中得到一个子字符串? 假设字符串为:从此实例检索子字符串。子字符串从指定的字符位置开始 子字符串结果我想显示的是:子字符串从指定的字符位置开始 将为您提供从位置0到位置9的前10个字符 看 下面是从14个字符到字符串末尾获取子字符串的示例。您可以修改它以满足您的需要 string text = "Retrieves a substring from this instance. The substring starts at a spec
看 下面是从14个字符到字符串末尾获取子字符串的示例。您可以修改它以满足您的需要
string text = "Retrieves a substring from this instance. The substring starts at a specified character position.";
//get substring where 14 is start index
string substring = text.Substring(14);
您可以手动或使用IndexOf方法执行此操作 手动:
int index = 43;
string piece = myString.Substring(index);
使用IndexOf,您可以看到fullstop的位置:
int index = myString.IndexOf(".") + 1;
string piece = myString.Substring(index);
丽雅
假设您希望在句号处拆分,那么下面是一种捕获所有事件的方法:
// add @ to the string to allow split over multiple lines
// (display purposes to save scroll bar appearing on SO question :))
string strBig = @"Retrieves a substring from this instance.
The substring starts at a specified character position. great";
// split the string on the fullstop, if it has a length>0
// then, trim that string to remove any undesired spaces
IEnumerable<string> subwords = strBig.Split('.')
.Where(x => x.Length > 0).Select(x => x.Trim());
// iterate around the new 'collection' to sanity check it
foreach (var subword in subwords)
{
Console.WriteLine(subword);
}
享受
string text = "Retrieves a substring from this instance. The substring starts at a specified character position. Some other text";
string result = text.Substring(text.IndexOf('.') + 1,text.LastIndexOf('.')-text.IndexOf('.'))
这将剪切位于特殊字符之间的字符串部分
var data =" Retrieves a substring from this instance. The substring starts at a specified character position.";
var result = data.Split(new[] {'.'}, 1)[0];
输出:
从此实例检索子字符串。子字符串从a开始
指定的字符位置
如果您的值介于2个子字符串之间,则此方法有效
例如:
电话应为:
myNameValue = SplitStringByASubstring(stringContent , "[myName]")
colorValue = SplitStringByASubstring(stringContent , "[color]")
etcValue = SplitStringByASubstring(stringContent , "[etc]")
为什么要使用子字符串?也许有更好的方法可以满足您的需要,然后使用带有幻数的子字符串,但是由于您没有告诉我们您的主要目标是什么,因此很难提出更好的方法。+1表示还建议了一种不使用幻数的方法。虽然真正的问题是不清楚需要做什么以及为什么。我建议int index=1+myString.IndexOf。;字符串片段=myString.Substringindex;略去“.”阿德里安,很抱歉,我通常不会轻易地投反对票,但我不得不这样做。这不仅有点“不安全”,你也没有任何逻辑来解释为什么你选择了魔法14,事实上它让我们处于“来自这个的字符串…”。我就是不明白。再次,对此表示抱歉,但我觉得我必须指出这一点。14只是一个随机数,我不觉得我应该为Riya计算字符串的字符数,他自己也可以这么做。。。我的答案只是一个如何获取字符串部分的示例,我不打算给出完整的解决方案。在我看来,对于这些简单的问题,指向正确的方向比给出完整的复制和粘贴代码要好…adrian-我的急躁是基于这样一个事实,即问题明确地提出:子字符串结果我想要显示的是:子字符串从指定的字符位置开始。因此,除了将子字符串作为问题的字面解释外,我看不到你答案中的相关性。也就是说,我明白你的意思,你给出的是指针,而不是完整的答案。现在一切顺利好了,我已经编辑了我的答案,所以很明显,这是使用子字符串的唯一示例:首先,您应该将代码作为文本发布,图像可以断开链接,无法搜索。第二,如果你认为你的答案是正确的,你应该使用OP的样本
stringContent = "[myName]Alex[myName][color]red[color][etc]etc[etc]"
myNameValue = SplitStringByASubstring(stringContent , "[myName]")
colorValue = SplitStringByASubstring(stringContent , "[color]")
etcValue = SplitStringByASubstring(stringContent , "[etc]")