Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查字符串是否为字符串模式_C# - Fatal编程技术网

C# 检查字符串是否为字符串模式

C# 检查字符串是否为字符串模式,c#,C#,我有以下字符串模式: const string STRING_PATTERN = "Hello {0}"; 如何检查字符串是否属于上述字符串模式 例如: 字符串“Hello World”属于上述字符串模式 字符串“abc”不属于上述字符串模式 最好使用正则表达式 Regex.IsMatch(myString, "^Hello .+$") 或者正如@usr所建议的: myString.StartsWith("Hello ") 你能写一个简单的示例代码吗?str.StartsWith(“Hel

我有以下字符串模式:

const string STRING_PATTERN = "Hello {0}";
如何检查字符串是否属于上述字符串模式

例如:

字符串“Hello World”属于上述字符串模式

字符串“abc”不属于上述字符串模式


最好使用正则表达式

Regex.IsMatch(myString, "^Hello .+$")
或者正如@usr所建议的:

myString.StartsWith("Hello ")

你能写一个简单的示例代码吗?
str.StartsWith(“Hello”)
就足够了吗?如果没有,您到底需要什么?如果我使用StartWith,它将无法与以下模式“Hello{0},Welcome to ABC”@e1011892:您确实需要首先充实您对该表达式的需求。假设我有一个未知的字符串模式,我如何检查该模式的字符串?
Regex.IsMatch(myString,pattern)
如果模式是正则表达式。如果您的格式中有一个模式,您可以先将其转换为正则表达式,或者编写自己的解析器。我是正则表达式新手,您能否编写一个示例代码,将以下模式“Hello{0}”({0}是一个数字)转换为正则表达式?@e1011892:在线查找正则表达式生成器。
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();
    }
  }
}