views:

67

answers:

3

I have a string that contains <name>James Jones</endofname>

how would i get the name from the centre pragmatically.

Thanks

James

A: 

You insist this is not XML parsing but your example looks an awful lot like it. So what is it?

Going on the assumption that this really is an XML document (because your example screams "yes it is!"), NSXMLParser is by far your best bet.

Joshua Nozzi
i just dont like the objective c xmlparser i have no knowledge of it, my data is extremely similar to xml
That's a poor excuse. If you don't know how to use the right tool for the job, then your first priority should be to learn it.
Joshua Nozzi
+1  A: 

For some reason you've specified that you don't want xml parsing. I don't know why this is a requirement, but I'd suggest using a regex then:

^[^>]*>([^<]*)

is a somewhat crude one but it'll get you started.

Clint Tseng
xcode does not fully support regex
Joshua Nozzi
<name>James Jones</endofname> there will sometimes be like <name2>asdasd adasdsa</endofname2> so this would not work
Why wouldn't it? The regex is crude I'll be the first to admit, but all it cares about is taking all the content between the > and the <. If you're looking for non-brute-force ways to do this, and you've ruled out regular expressions, this is the best I can think of. Alternatively, split on >, take the second half, then split on < and take the first, but that seems non-ideal.
Clint Tseng
A: 

For this particular example, you can write [string substringWithRange:NSMakeRange(6, 11)]. For more complicated examples, you'd need to know the full extent of the language you're parsing and create a parser for it.

Chuck