【问题标题】:Find String with BeginsWith and EndsWith使用 BeginsWith 和 EndsWith 查找字符串
【发布时间】:2015-07-10 14:40:51
【问题描述】:

我想在一个长字符串中搜索一个 BeginsWith 和 EndsWith 的字符串,然后最终用一个标记替换它。

所以,假设我有一个字符串:

"This is text with a token: <TokenID=123>doesn't matter</Token>, and some more text!"

我想提取/识别以下字符串:

<TokenID=123>doesn't matter</Token>

这样我就可以在原始字符串的替换语句中使用它,用其他东西替换它。此标签的 ID 可能不同,因此我想通过以下方式识别上面的字符串:

var beginsWith = "<TokenID=";
var endsWith = "</Token>";

BeginsWith 和 EndsWith 值将从 CSV 文件中提取到列表中,因为其中有许多具有不同的内容。一旦我知道如何提取这些,我最终想用一个可以拆分的字符替换它们,以围绕提取的字符串创建一个字符串数组。

我不想使用正则表达式,因为 BeginsWith 和 EndsWith 字符串需要易于配置并在以后添加。

感觉这应该是一个非常简单的练习,但对于我的生活,我无法弄清楚如何去做......

【问题讨论】:

  • 可能想使用正则表达式。类似&lt;TokenID=[0-9]*&gt;.*&lt;/Token&gt;
  • 你在哪里看到Hello there?此外,如果它是beginsWith "&lt;TokenID=",您还想包括123&gt;Hello there。请告诉我们具体的规则。您不想提取123
  • 您在寻找String.IndexOf() 吗?
  • 嗨蒂姆,对不起,我在发布之前更改了字符串,我现在已经修复了
  • 我想检索第二个块中的所有内容,使用 BeginsWith 和 EndsWith 作为搜索

标签: c# string


【解决方案1】:

如果我对您的问题的理解正确,您会想要使用IndexOf()Substring()

IndexOf() 将为您提供 beginWith 和 endWith 的位置,您可以将其提供给Substring()

string data = "This is text with a token: <TokenID=123>doesn't matter</Token>, and some more text!";
string beginsWith = "<TokenID=";
string endsWith = "</Token>";

int startIndex = data.IndexOf(beginsWith);
// Add the length of endWidth so you're getting the location of the last character of the endsWith
int endIndex = data.IndexOf(endsWith) + endsWith.Length;
string extract = data.Substring(startIndex, endIndex - startIndex);
Console.WriteLine(extract);
Console.ReadLine();

结果:

<TokenID=123>doesn't matter</Token>

如果您改变了使用 Regex 的想法,您仍然可以使用您的 beginWith 和 endsWith 来创建您的模式。

string data = "This is text with a token: <TokenID=123>doesn't matter</Token>, and some more text!";
string beginsWith = "<TokenID=";
string endsWith = "</Token>";

string extract = Regex.Match(data, String.Format("{0}.+{1}", beginsWith, endsWith)).Value;
Console.WriteLine(extract);
Console.ReadLine();

String.Format() 创建的模式看起来像

<TokenID=.+</Token>

结果:

<TokenID=123>doesn't matter</Token>

【讨论】:

  • 拖钓投票?请评论给定答案的否决票。
  • 我选择了这个,因为它们几乎都是相同的答案,但这是第一个并且有效。谢谢
【解决方案2】:

这是一个扩展方法,因此您可以将其附加到字符串:

方法

    private static string StringBetween( this string StringEval, string startsWith, string endsWith)
    {
        var str = StringEval;
        var start = str.IndexOf(startsWith);
        var end = str.IndexOf(endsWith, start);


        var val = str.Substring(start, (end - start) + endsWith.Length);
        return val;
    }

用法

static void Main(string[] args)
        {
           var exampleStr = "This is text with a token: <TokenID=123>doesn't matter</Token>, and some more text!";
           var startsWith = "<TokenID=";
           var endsWith = "</Token>";

            var val = exampleStr.StringBetween(startsWith, endsWith);

            Console.WriteLine(val);

            Console.ReadKey();
        }

【讨论】:

    【解决方案3】:

    不使用正则表达式:

    string s="This is text with a token: <TokenID=123>doesn't matter</Token>, and and some more text!" 
    string beginsWith = "<TokenID=";
    string endsWith   = "</Token>";
    
    string Extract    = null ;
    int    i,j ;
    if ((i=s.IndexOf(beginsWith))>=0) && ((j=s.Substring(i).IndexOf(endsWith)>=0))
      Extract=s.Substring(i,j)+endsWith ;
    // result in extract (null if delimiting strings not found)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-17
      • 1970-01-01
      • 2012-01-18
      • 1970-01-01
      • 2011-09-04
      相关资源
      最近更新 更多