String issues

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi,
i know this might sound absurd but does anyone know how to search fora
particular pattern of text within a string and remove that every text from
the beginning of the string to that pattern(inclusive) and also just remove
that pattern alone from the whole text


e.g

"this is the arbitrary text that the *string* is in "

the result of the operation should give:

"is in "

hence the rest of the text has been removed


i know this can be done using regex.replace(), but i dont want to use that
option, i want to be able to do this vie substring() or stringBuilder
(preferably substring)

thanks
 
Hi,

you may want to look at the String.IndexOf and String.Remove methods.

example:

string s = "this is the arbitrary text that the *string* is in ";
string separator = "*string*";
string newS = s.Substring(s.IndexOf(separator) + separator.Length);
string newS2 = s.Remove(s.IndexOf(separator), separator.Length);

Only works if your pattern is a fixed string, else you will have to look
at the Regex-Class.

HTH,
Stefan
 
thanks stefan, yep i did know that solution but it will only work for a fixed
string mine is not so i guess i have to use the regex class, i did think it
was possible to do this using string methods only...oh well thanks anyway
 
ShadowOfTheBeast said:
thanks stefan, yep i did know that solution but it will only work for a fixed
string mine is not so i guess i have to use the regex class, i did think it
was possible to do this using string methods only...oh well thanks anyway

In what way will it only work for a fixed string? You can make the
"separator" variable in Stefan's code have whatever value you want.

It could be that we're talking about different meanings of "fixed
string" though, of course...
 
Hi Jon,
what i actually mean by fixed string is a string Pattern..more specifically
a regular expression match e.g *Any string that is between this asterix*,
hence not a particular text, just a match of any text that fits between those
markers.
 
Hi Jon,
so if i wanted the seperator variable to be a "match" or a string
pattern...hence a regex match how would i then implement that without using
the regex class?
 
ShadowOfTheBeast said:
what i actually mean by fixed string is a string Pattern..more specifically
a regular expression match e.g *Any string that is between this asterix*,
hence not a particular text, just a match of any text that fits between those
markers.

That sounds like what you're actually meaning is "get all the text
before the first asterisk and after the second asterisk" then, which
could easily be done with IndexOf.

Maybe I'm still missing something though...
 
ShadowOfTheBeast said:
so if i wanted the seperator variable to be a "match" or a string
pattern...hence a regex match how would i then implement that without using
the regex class?

I'm still not entirely sure I'm clear what you want, but does this
help?

string FindStringAfterPattern (string data, string separator)
{
string starredSeparator = "*"+separator+"*";
int index = data.IndexOf(starredSeparator);
if (index==-1)
{
// No separator - not sure what you want to do here...
return null;
}

return data.Substring (index+starredSeparator.Length);
}
 
Back
Top