C#在字符串中查找文本,然后返回其余文本

C#在字符串中查找文本,然后返回其余文本,c#,string,substring,C#,String,Substring,我正在尝试这样做: string foo = "Hello, this is a string"; //and then search for it. Kind of like this string foo2 = foo.Substring(0,2); //then return the rest. Like for example foo2 returns "He". //I want it to return the rest "llo, this is a string" 谢谢。我想你

我正在尝试这样做:

string foo = "Hello, this is a string";
//and then search for it. Kind of like this
string foo2 = foo.Substring(0,2);
//then return the rest. Like for example foo2 returns "He".
//I want it to return the rest "llo, this is a string"

谢谢。

我想你应该澄清一下规则,当你需要另一个字符串时,你想要什么

        string foo = "Hello, this is a string";
        int len1 = 2; // suppose this is your rule
        string foo2 = foo.Substring(0, len1);
        string foo3 = foo.Substring(len1, foo.Length - len1); //you want this?
结果:

//He
//llo, this is a string
//He
//llo, this is a string
如果您需要一直这样做,您可以创建一个

扩展方法:

public static class Extensions
{
    public static Tuple<string, string> SplitString(this string str, int splitAt)
    {
        var lhs = str.Substring(0, splitAt);
        var rhs = str.Substring(splitAt);
        return Tuple.Create<string, string>(lhs, rhs);
    }   
}
结果:

//He
//llo, this is a string
//He
//llo, this is a string

你应该试试这样的

publicstringfindReturnRest(stringsourcestr,stringstrtofind)
{
返回sourceStr.Substring(sourceStr.IndexOf(strToFind)+strToFind.Length);
}
然后

string foo=“你好,这是一个字符串”;
字符串rest=find和returnrest(foo,“He”);

将其更改为
foo.Substring(2)。完成。