检查一个字符串是一个字符串模式
问题描述:
的,我有以下字符串模式:检查一个字符串是一个字符串模式
const string STRING_PATTERN = "Hello {0}";
如何检查一个字符串是上面的字符串模式的?
例如:
字符串“Hello World”是上面的字符串模式
字符串“abc”的是上面的字符串模式不。
最好
答
使用正则表达式。
Regex.IsMatch(myString, "^Hello .+$")
或者为@usr建议:
myString.StartsWith("Hello ")
答
using System;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string txt="Hello World";
string re1="(Hello)"; // Word 1
string re2=".*?"; // Non-greedy match on filler
string re3="((?:[a-z][a-z]+))"; // Word 2
Regex r = new Regex(re1+re2+re3,RegexOptions.IgnoreCase|RegexOptions.Singleline);
Match m = r.Match(txt);
if (m.Success)
{
String word1=m.Groups[1].ToString();
String word2=m.Groups[2].ToString();
Console.Write("("+word1.ToString()+")"+"("+word2.ToString()+")"+"\n");
}
Console.ReadLine();
}
}
}
你可以写一个简单的示例代码? – e1011892 2013-04-08 19:42:41
'str.StartsWith(“你好”)'会够吗?如果不是,你到底需要什么? – usr 2013-04-08 19:42:54
如果我使用StartsWith,它将无法正确使用以下模式“您好{0},欢迎来到ABC” – e1011892 2013-04-08 19:44:46