Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取字符串中的特定子字符串位置_C#_String_Substring - Fatal编程技术网

C# 如何获取字符串中的特定子字符串位置

C# 如何获取字符串中的特定子字符串位置,c#,string,substring,C#,String,Substring,我有一个字符串121712 1512 0025 64559 7259181568 701 013 2。现在根据我的要求,我必须得到最后一个子字符串2StartIndex和EndIndex。但这里的问题是2在第一个子字符串中出现121712所以它从那里拿走了我不想要的东西。。 这是我试过的代码 string s = "121712 1512 0025 64559 7259181568 701 013

我有一个字符串
121712 1512 0025 64559 7259181568 701 013 2
。现在根据我的要求,我必须得到最后一个子字符串
2
StartIndex
EndIndex
。但这里的问题是
2
在第一个子字符串中出现
121712
所以它从那里拿走了我不想要的东西。。 这是我试过的代码

        string s = "121712  1512  0025            64559         7259181568        701  013              2 ";

        string inout = "2";

        string[] arrs = s.Split(' ');

        for (int i = 0; i <= arrs.Length; i++)
        {
            string str = arrs[i];
            if (str == inout)
            {

                int startIndex = s.IndexOf(arrs[i]);

                int endIndex = s.IndexOf(" ", startIndex);

                int difference = endIndex - startIndex;

                string inoutSubstring = (startIndex + "," + difference);
            }
        }
string s=“121712 1512 0025 64559 7259181568 701 013 2”;
字符串inout=“2”;
字符串[]arrs=s.Split(“”);

对于(int i=0;i如果我理解正确,您希望查找字符串中子字符串的最后关联。C#中的
string
对象有一个专门为此设计的方法,
string.LastIndexOf

string s = "121712  1512  0025            64559         7259181568        701  013              2 ";

string inout = "2";

string[] arrs = s.Split(' ');

for (int i = 0; i <= arrs.Length; i++)
{
     string str = arrs[i];
     if (str == inout)
     {
         int startIndex = s.IndexOf("2");
         int endIndex = s.LastIndexOf("2");
     }
 }
string s=“121712 1512 0025 64559 7259181568 701 013 2”;
字符串inout=“2”;
字符串[]arrs=s.Split(“”);

对于(int i=0;i你说的最后一个子串是对的

s、 LastIndexOf(inout)将给出inout字符串的起始索引。现在我需要在s字符串中获取inout的endindex。将inout.Lenght-1添加到s.LastIndexOf(inout)中,它将给出inout字符串的最后一个索引

您可以为任何字符串指定新的sting int inout变量,如“21”、“12”或许多其他变量

        string inout = "2";
        int startIndex;
        int endIndex;
        startIndex = s.LastIndexOf(inout);
        endIndex = startIndex + inout.Length-1;

看起来不是这样C@ThisaruGuruge对不起,我的错误发生在c上,实际上它是c#OK。让我更清楚地说明一下。假设子字符串是
21
而不是
2
,它可以作为单独的子字符串出现在整个字符串中的任何位置。在这种情况下,我的目标是获取startindex和endindex,这样我就可以将这个子字符串从b中取出给我这个numbers@HansalMehta这很好-只要将
21
替换为
2
。这两种方法都接受一个普通的旧字符串。实际上,我关心的是
2
也存在于
121712
中。如果我试图找到
2
的索引,它总是从第一次出现时即从
121712中得到它e> 然而,我们希望它从最后一个地方或任何其他地方,那里只有
2
,同样的应用程序在
23
的例子中,先生…?@HansalMehta是的,正如我在帖子中解释的,这就是
LastIndexOf
的全部要点。对于
121712
IndexOf(“2”)
将返回
1
,而
LastIndexOf(“2”)
将返回
5
。谢谢您理解我的问题。我想再指出一件事,假设inout VARIABLE value位于字符串的中间或除最后一个位置之外的其他位置,假设您给出了解决方案,在这种情况下,该解决方案将如何工作?是的……它将工作……您的目标是u想要得到最后一个子串…如果相同的子串出现在开始或中间,你不需要,你只需要最后一个子串…谢谢…它工作了