次の文字列パターンがあります。
const string STRING_PATTERN = "Hello {0}";
文字列が上記の文字列パターンであることを確認するにはどうすればよいですか?
例えば:
文字列 "Hello World" は上記の文字列パターンです
文字列「abc」は上記の文字列パターンではありません。
一番
次の文字列パターンがあります。
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();
}
}
}