删除字符串中的所有文本,直到达到多个字符匹配

时间:2012-11-28 14:29:06

标签: c#

我希望在匹配后删除所有内容:

/**

我的字符串是:

  

/ r / _ylt = A0oG7lyFHLZQjSEAWltXNyoA; _ylu = X3oDMTE0NGYxbHRnBHNlYwNzcgRwb3MDOQRjb2xvA2FjMgR2dGlkA1ZJUDA0OV83Nw - / SIG = 11pqblim6 / EXP = 1354140933 / ** http%3a // www.hellomagazine.com / royalty /“target =”_ blank“data-bk =” 5254.1“dirtyhref =”/ r / _ylt = A0oG7lyFHLZQjSEAWltXNyoA; _ylu = X3oDMTE0NGYxbHRnBHNlYwNzcgRwb3MDOQRjb2xvA2FjMgR2dGlkA1ZJUDA0OV83Nw - / SIG = 11pqblim6 / EXP = 1354140933 / ** http%3a // www.hellomagazine.com / royalty /

我想删除/**

之前的所有内容

我可以匹配1个字符,但我怎么能为多个字符做?

5 个答案:

答案 0 :(得分:2)

你有什么尝试?你如何匹配一个角色,为什么不能将其改为多个角色?字符串操作完美记录。

你需要这样的东西:

string input = "your string/**result";
string separator = "/**";
int startPos = input.IndexOf(separator);

if (startPos == -1)
{
    return input;
}

return input.Substring(startPos + separator.Length);

如果您确实想要返回分隔符,请忽略+ separator.Length

答案 1 :(得分:2)

Substring的简单IndexOf有效:

string output = input.Substring(input.IndexOf(@"/**") + @"/**".Length);

答案 2 :(得分:0)

var inputstr = "your input string";
int index = inputstr.IndexOf(@"/**");
if (index > 0)
{
  var result = inputstr.Substring(index);
}

答案 3 :(得分:0)

假设您的字符串名为myString

myString = myString.Substring(myString.IndexOf("/**") + 1)

答案 4 :(得分:0)

class Program
{
    static void Main(string[] args)
    {
        string test="/r/_ylt=A0oG7lyFHLZQjSEAWltXNyoA;_ylu=X3oDMTE0NGYxbHRnBHNlYwNzcgRwb3MDOQRjb2xvA2FjMgR2dGlkA1ZJUDA0OV83Nw--/SIG=11pqblim6/EXP=1354140933/**http%3a//www.hellomagazine.com/royalty/\" target=\"_blank\" data-bk=\"5254.1\" dirtyhref=\"/r/_ylt=A0oG7lyFHLZQjSEAWltXNyoA;_ylu=X3oDMTE0NGYxbHRnBHNlYwNzcgRwb3MDOQRjb2xvA2FjMgR2dGlkA1ZJUDA0OV83Nw--/SIG=11pqblim6/EXP=1354140933/**http%3a//www.hellomagazine.com/royalty/";
        var value=test.Split(new string[]{"/**"},StringSplitOptions.None);
        //continue working with value[0], value[0] etc
    }
}