6

I am trying to get string between same strings:

The texts starts here ** Get This String ** Some other text ongoing here.....

I am wondering how to get the string between stars. Should I should use some regex or other functions?

6 Answers 6

8

You can try Split:

  string source = 
    "The texts starts here** Get This String **Some other text ongoing here.....";

  // 3: we need 3 chunks and we'll take the middle (1) one  
  string result = source.Split(new string[] { "**" }, 3, StringSplitOptions.None)[1];
Sign up to request clarification or add additional context in comments.

2 Comments

Did not know you could specify chunks! What's the benefit of explicitly specifying those?
@EpicKip: imagine, that you have, say, 10000 occurrencies of **. If you don't put 3, you'll get an array with as many as 10001 items (which will be time and space consuming); if you do - just with 3.
7

You can use IndexOf to do the same without regular expressions.
This one will return the first occurence of string between two "**" with trimed whitespaces. It also has checks of non-existence of a string which matches this condition.

public string FindTextBetween(string text, string left, string right)
{
    // TODO: Validate input arguments

    int beginIndex = text.IndexOf(left); // find occurence of left delimiter
    if (beginIndex == -1)
        return string.Empty; // or throw exception?

    beginIndex += left.Length;

    int endIndex = text.IndexOf(right, beginIndex); // find occurence of right delimiter
    if (endIndex == -1)
        return string.Empty; // or throw exception?

    return text.Substring(beginIndex, endIndex - beginIndex).Trim(); 
}    

string str = "The texts starts here ** Get This String ** Some other text ongoing here.....";
string result = FindTextBetween(str, "**", "**");

I usually prefer to not use regex whenever possible.

3 Comments

"**".Length instead of magic number 2 is a bit more accurate implementation
When wrapping logic into a public method, please, do not forget to validate its arguments (or at least, leave //TODO: validate text, left, right here comment). What if I call the routine as var result FindTextBetween(null, null, null);
@DmitryBychenko I have updated my answer :) Thank you.
3

If you want to use regex, this could do:

.*\*\*(.*)\*\*.*

The first and only capture has the text between stars.

Another option would be using IndexOf to find the position of the first star, check if the following character is a star too and then repeat that for the second set. Substring the part between those indexes.

Comments

2

If you can have multiple pieces of text to find in one string, you can use following regex:

\*\*(.*?)\*\*

Sample code:

string data = "The texts starts here ** Get This String ** Some other text ongoing here..... ** Some more text to find** ...";
Regex regex = new Regex(@"\*\*(.*?)\*\*");
MatchCollection matches = regex.Matches(data);

foreach (Match match in matches)
{
    Console.WriteLine(match.Groups[1].Value);
}

Comments

1

You could use split but this would only work if there is 1 occurrence of the word.

Example:

string output = "";
string input = "The texts starts here **Get This String **Some other text ongoing here..";
var splits = input.Split( new string[] { "**", "**" }, StringSplitOptions.None );
//Check if the index is available 
//if there are no '**' in the string the [1] index will fail
if ( splits.Length >= 2 )
    output = splits[1];

Console.Write( output );
Console.ReadKey();

Comments

0

You can use SubString for this:

String str="The texts starts here ** Get This String ** Some other text ongoing here";

s=s.SubString(s.IndexOf("**"+2));
s=s.SubString(0,s.IndexOf("**"));

1 Comment

There's a syntax error: s.IndexOf("**"+2) is wrong, correct is s.IndexOf("**") + 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.