10

I have some string

   bla bla bla bla  <I NEED THIS TEXT> 

What is the best and fastest way to get text inside <>?

Alex Aza
  • 76,499
  • 26
  • 155
  • 134
Ievgen
  • 4,261
  • 7
  • 75
  • 124
  • 1
    The problem with the `IndexOf`-solutions is the "bla bla > bla bla " text. In tha case the end – Aaaaaaaa May 31 '11 at 09:24
  • 3
    @Csillik David Janos: Not when properly handled - most examples here give a rough idea of how to achieve a goal, not cover every possible aspect; it can be assumed the OP would tailor answers to best suit needs. – Grant Thomas May 31 '11 at 10:10
  • 1
    There are a lot of possible aspects, thats why are there the regexps – Aaaaaaaa May 31 '11 at 10:40
  • 1
    @Csillik David Janos: Regexes aren't there to be abused. No need for such an expensive mechanism for simple tasks - ever used a sledgehammer to crack a nut? - I'm not dismissing their place, but they're not to be cast around at whim. – Grant Thomas May 31 '11 at 15:53

9 Answers9

6
int start = s.IndexOf("<") + 1;
int end = s.IndexOf(">",start);

string text = s.Substring(start,end - start);
Stecya
  • 22,896
  • 10
  • 72
  • 102
3

Use Substring with the indexes of < and >, obtained by IndexOf.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
3
var s = "bla bla bla bla  <I NEED THIS TEXT> ";
Console.WriteLine(s.Substring(s.IndexOf('<') + 1, s.IndexOf('>') - s.IndexOf('<') - 1));
Alex R.
  • 4,664
  • 4
  • 30
  • 40
3
Regex.Match(input, "<(.*)>").Groups[1].Value
chopikadze
  • 4,219
  • 26
  • 30
2
var input = "bla bla bla bla  <I NEED THIS TEXT> ";

var match = Regex.Match(input, @".*?<(?<MyGroup>.*?)>");
if (match.Success)
    var text = match.Groups["MyGroup"].Value;
Alex Aza
  • 76,499
  • 26
  • 155
  • 134
  • This answer with regex was first and i checked it and its working. – Ievgen May 31 '11 at 12:26
  • 1
    @Evgeny - Thank you for the credit. I actually didn't post my answer until I checked it was working. Sad to find sometimes that some people just downvote without explanation. Also, I noticed that whenever question about parsing is asked, people tend to upvote non-regex answers. – Alex Aza May 31 '11 at 16:20
  • 1
    @Csillik David Janos - if you think that it is wrong, you could check if code is working before commenting. – Alex Aza May 31 '11 at 16:22
  • First of all, it was a question. But you have right, it works fine! – Aaaaaaaa Jun 01 '11 at 09:40
2

Will there be nesting? Two of the answers above will give different results:

static void Main()
{
    string s = "hello<I NEED <I NEED THIS TEXT> THIS TEXT>goodbye";

    string r = Regex.Match(s, "<(.*)>").Groups[1].Value;

    int start = s.IndexOf("<") + 1;
    int end = s.IndexOf(">", start);
    string t = s.Substring(start, end - start);

    Console.WriteLine(r);
    Console.WriteLine(t);

    Console.ReadKey();
}
Einar
  • 312
  • 1
  • 10
1

Without regex, and checking of sorts:

var data = "bla bla bla bla <I NEED THIS TEXT>";

int start = 0, end = 0;
if ((start = data .IndexOf("<")) > 0 &&
    (end = data .IndexOf(">", start)) > 0)
{
    var result = data .Substring(start + 1, end - start - 1);
}
Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
1

Using string.SubString and IndexOf mwthods will only work is "<" and ">" are the start and end of the text you want. If these characters happen to be included before the actual text starts then you wont get correct string.

The best thing is to use regular expressions.

BGohil
  • 165
  • 1
  • 2
  • 5
0
var input = "bla bla bla bla  <I NEED THIS TEXT>";
Match match = Regex.Match(input, @"<([^>]*)>");
if (match.Success)
{
    // Do some with match.Groups[1].Value;
}
Aaaaaaaa
  • 2,015
  • 3
  • 22
  • 40