简体   繁体   English

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

[英]How to Read Substrings based on Length of the String in C#

I have a String which contains a substrings One of them is "1 . 2 To Other Mobiles" and other is "Total".Now as per my requirement i have to read the Contents between first substring ie ."1 . 2 To Other Mobiles" and "Total".I am doing it by following code in c#. 我有一个字符串,其中包含一个子字符串其中一个是“1.2 To Other Mobiles”,另一个是“Total”。现在根据我的要求,我必须阅读第一个子字符串之间的内容,即“.1。2到其他手机“和”总计“。我是通过遵循c#中的代码来实现的。

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

But the problem that i am facing is "Total" is Many times in my substring..I have to read after the startPosition length to last position length ie "Total". 但是我面临的问题是“Total”在我的子串中很多次。我必须在startPosition长度之后读取最后一个位置长度,即“Total”。 How to do it? 怎么做?

I have to read after the startPosition length to last position length 我必须在startPosition长度后读取最后一个位置长度

Use LastIndexOf : 使用LastIndexOf

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);
    }
}

What should i do if i need to read for the first "Total" only. 如果我只需要阅读第一个“总计”,我该怎么办?

then use IndexOf instead: 然后使用IndexOf代替:

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

try this. 尝试这个。

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

你可以使用String.LastIndexOf()

Problem : you are not adding the length of the first search string 1 . 2 To Other MobilessubstringTotal 问题:您没有添加第一个搜索字符串1 . 2 To Other MobilessubstringTotal的长度1 . 2 To Other MobilessubstringTotal 1 . 2 To Other MobilessubstringTotal

Solution : 方案:

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));

Output: 输出:

result => substring result => substring

You mean this? 你是这个意思?

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));

In the text "1 . 2 To Other Mobiles fkldsjkfkjslfklsdfjk Total"; 在文本“1.2 To Other Mobiles fkldsjkfkjslfklsdfjk Total”中;

The output will be: 输出将是:

fkldsjkfkjslfklsdfjk fkldsjkfkjslfklsdfjk

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM