C# finding index of 3rd instance of "}"

  • Thread starter Thread starter NuBBeR
  • Start date Start date
N

NuBBeR

I need to know how to grab a substring between "{" and "}". The full text
has many instances of these brackets and i need to find the string
"{\\colortbl\\red0\\blue0\\green0;\\red255\\blue0\\green0\\;}" PLEASE help.
very urgent. Thanks in advance!
 
NuBBeR said:
I need to know how to grab a substring between "{" and "}". The full text
has many instances of these brackets and i need to find the string
"{\\colortbl\\red0\\blue0\\green0;\\red255\\blue0\\green0\\;}" PLEASE help.
very urgent. Thanks in advance!

Use string.IndexOf(string, int) using 0 as the first starting position,
and thereafter starting from one place beyond the last match.
 
The thing is i dont know how many times i need to use indexof(), until i
reach the brackets that im trying to get
for instance there might be 10 sets of brackets before i reach the pair i
need or there might be only 1.
 
NuBBeR said:
The thing is i dont know how many times i need to use indexof(), until i
reach the brackets that im trying to get
for instance there might be 10 sets of brackets before i reach the pair i
need or there might be only 1.

Well, what *do* you know about the pair you need?

If you need to find a specific string, why not just use IndexOf with
the whole string?
 
Ok, I am trying to parse RTF code, it has
{blahblahblah}{a bunch of
defenitions}{abunchmore}...{\\colortbl\\re....}{blahblahblah}...

the {\\colortbl\\re...} is the one i have to find because i am going to
split it. It contains colors that are used in the file. So all that said,
all i want is to grab that substring out of the text and store it as a
string, the only problem i am having is i could use
SubStr(str.IndexOf("{\\colortbl...}")) but i cant use any more parameters
because 1) i dont know how long the string is going to be because it isnt
constant 2) the following brackets arent always the same. I really do
appreciate the help.
 
NuBBeR said:
Ok, I am trying to parse RTF code, it has
{blahblahblah}{a bunch of
defenitions}{abunchmore}...{\\colortbl\\re....}{blahblahblah}...

the {\\colortbl\\re...} is the one i have to find because i am going to
split it. It contains colors that are used in the file. So all that said,
all i want is to grab that substring out of the text and store it as a
string, the only problem i am having is i could use
SubStr(str.IndexOf("{\\colortbl...}")) but i cant use any more parameters
because 1) i dont know how long the string is going to be because it isnt
constant 2) the following brackets arent always the same. I really do
appreciate the help.

So use str.IndexOf ("{\\colortbl") to find the start of it, and then
use str.IndexOf ("}", start) to find the closing bracket.
 
NuBBeR said:
I need to know how to grab a substring between "{" and "}". The full text
has many instances of these brackets and i need to find the string
"{\\colortbl\\red0\\blue0\\green0;\\red255\\blue0\\green0\\;}" PLEASE help.
very urgent. Thanks in advance!

Here is one way but its not efficient at all.

string[] bracket_list = full_text.split('{'); // This will split the
text into an array of length depending on how many '{' you have in the
text you send it, in this case full_text.

foreach(string str in bracket_list)
{
// Check to see if the string you desire is the same as the string
str.
}

Hope this helps...but its not efficient at all. It could be quite
memory intensive if you have a lot of '{'.

There is a way to walk through the string and decrement items out of
it in a while loop and look at the string broken out of the complete
string but I'm too lazy to actually try code that right now. Maybe
someone else can do that if you need it. :-)
 
Back
Top