1

I want to search a long string for a string inside it that BeginsWith and EndsWith something, and then eventually replace it with a token.

So, let's say I have a string:

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

I'd like to extract/identify the following string:

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

So that I can use it within a replace statement on the original string, to replace it with something else. The ID for this tag could be different, so I want to identify the string above by using something like:

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

The BeginsWith and EndsWith values will be pulled from a CSV file into a list, as there are many of them with different content. Once I know how to extract these, I eventually want to replace them with a character that I could split on to create an array of strings around the extracted strings.

I don't want to use regex, as the BeginsWith and EndsWith strings need to be easily configurable and added to at a later stage.

This feels like it should be a really simple exercise, but for the life of me I can't figure out how to do it...

Mark
  • 315
  • 2
  • 8
  • 24

3 Answers3

1

If I'm understanding your question correctly, you're going to want to use IndexOf() & Substring().

IndexOf() will get you the locations of your beginsWith and endWith for which you can provide to the 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();

Results:

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

If you change your mind about using Regex, you can still use your beginsWith and endsWith to create your pattern.

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();

The String.Format() creates a pattern that looks like

<TokenID=.+</Token>

Results:

<TokenID=123>doesn't matter</Token>
Shar1er80
  • 9,001
  • 2
  • 20
  • 29
0

Here's an extension method so you can just attach it to a string:

Method

    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;
    }

Usage

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();
        }
ZeroBased_IX
  • 2,667
  • 2
  • 25
  • 46
0

Without using Regex:

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)
Graffito
  • 1,658
  • 1
  • 11
  • 10