How to determine if string contains specific substring within the first X characters

前端 未结 10 1610
一生所求
一生所求 2020-12-08 09:06

I want to check whether Value1 below contains \"abc\" within the first X characters. How would you check this with an if statement?



        
10条回答
  •  南笙
    南笙 (楼主)
    2020-12-08 09:44

    You can also use regular expressions (less readable though)

    string regex = "^.{0,7}abc";
    
    System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(regex);
    string Value1 = "sssddabcgghh";
    
    Console.WriteLine(reg.Match(Value1).Success);
    

提交回复
热议问题