C# 如何在C中根据字符串的长度读取子字符串#

C# 如何在C中根据字符串的长度读取子字符串#,c#,regex,string,substring,C#,Regex,String,Substring,我有一个字符串,其中包含一个子字符串,其中一个是“1.2到其他手机”,另一个是“总计”。现在根据我的要求,我必须读取第一个子字符串之间的内容,即“1.2到其他手机”和“总计”。我按照c#中的代码进行操作 但是我面临的问题是“Total”在我的子字符串中有很多次。我必须在起始位置长度之后读取到最后一个位置长度,即“Total”。 怎么做?你可以用这个试试 int startPosition = currentText.IndexOf("1 . 2 To Other Mobiles"); int e

我有一个字符串,其中包含一个子字符串,其中一个是“1.2到其他手机”,另一个是“总计”。现在根据我的要求,我必须读取第一个子字符串之间的内容,即“1.2到其他手机”和“总计”。我按照c#中的代码进行操作

但是我面临的问题是“Total”在我的子字符串中有很多次。我必须在起始位置长度之后读取到最后一个位置长度,即“Total”。 怎么做?

你可以用这个试试

int startPosition = currentText.IndexOf("1 . 2 To Other Mobiles");
int endPosition = currentText.LastIndexOf("Total") + 5;
string result = currentText.Substring(startPosition, endPosition - startPosition);

问题:您没有添加第一个搜索字符串的长度
1。2至其他手机SubstringTotal

解决方案:

string currentText = "1 . 2 To Other MobilessubstringTotal";
string search1="1 . 2 To Other Mobiles";
string search2="Total";
int startPosition = currentText.IndexOf(search1);
int endPosition = currentText.IndexOf(search2);
string result = currentText.Substring((startPosition+search1.Length), endPosition - (startPosition+search1.Length));
输出:

string currentText = "1 . 2 To Other MobilessubstringTotal";
string search1="1 . 2 To Other Mobiles";
string search2="Total";
int startPosition = currentText.IndexOf(search1);
int endPosition = currentText.IndexOf(search2);
string result = currentText.Substring((startPosition+search1.Length), endPosition - (startPosition+search1.Length));
结果=>
子字符串

你是说这个

string currentText = "1 . 2 To Other Mobiles fkldsjkfkjslfklsdfjk  Total";
string text = "1 . 2 To Other Mobiles";
int startPosition = currentText.IndexOf("1 . 2 To Other Mobiles");
int endPosition = currentText.IndexOf("Total");
string result = currentText.Substring(startPosition + text.Length, endPosition - (startPosition + text.Length));
在文本“1.2至其他手机FKLDSJKKFJSLFKLSDFJK总计”

输出将是:

FKLDSJKKKJSLFKLSDFJK

我必须在开始位置长度后阅读到最后位置长度

使用:

如果我只需要阅读第一个“总计”,我该怎么办

然后改用:


@Adi,它是
“总长度”
。添加它是为了让文本“Total”本身也包含在结果中。@CompuChip是正确的,我已经静态输入了5,而不是使用“Total”。Length;这就是为什么我更喜欢使用变量,比如
来搜索
,这样我就可以在不破坏代码的情况下更改我想要搜索的内容。它也更可编辑。谢谢您的回复。在给定的代码中,我可以获得直到最后一个“总计”的数据。如果我只需要阅读第一个“总计”,我该怎么办。@Adi:然后使用
IndexOf
,编辑我的答案。如果您需要所有位置,您需要一个循环,只要
IndexOf(“Total”,lastIndex+1)>=0
。如果(endPosition>startPosition)这个条件永远不会满足,因为“Total”也在…之前??为什么total在第一个子字符串之前?我以为你想找到两者之间的子串,而“Total”在“1.2到其他手机”之后。@TimSchmelter先生,我必须从“1.2到其他手机”读到直接的“Total”。因为“Total”既在前面,也在后面。所以需要在“1.2到其他手机”和立即的“Total”之间读
string search1 = "1 . 2 To Other Mobiles";
string search2 = "Total";

int startPosition = currentText.IndexOf(search1);
if(startPosition >= 0)
{
    startPosition += search1.Length;
    int endPosition = currentText.LastIndexOf(search2);
    if (endPosition > startPosition)
    {
        string result = currentText.Substring(startPosition, endPosition - startPosition);
    }
}
// ...
int endPosition = currentText.IndexOf(search2);
if (endPosition > startPosition)
{
    string result = currentText.Substring(startPosition, endPosition - startPosition);
}