By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,709 Members | 2,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,709 IT Pros & Developers. It's quick & easy.

String issues

P: n/a
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
--
The Matrix Insurrection
Nov 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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
ShadowOfTheBeast schrieb:
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

Nov 17 '05 #2

P: n/a
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

"Stefan L" wrote:
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
ShadowOfTheBeast schrieb:
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

Nov 17 '05 #3

P: n/a
ShadowOfTheBeast <Sh**************@discussions.microsoft.com> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

P: n/a
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.

"Jon Skeet [C# MVP]" wrote:
ShadowOfTheBeast <Sh**************@discussions.microsoft.com> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #5

P: n/a
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?

"Jon Skeet [C# MVP]" wrote:
ShadowOfTheBeast <Sh**************@discussions.microsoft.com> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #6

P: n/a
ShadowOfTheBeast <Sh**************@discussions.microsoft.com> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7

P: n/a
ShadowOfTheBeast <Sh**************@discussions.microsoft.com> wrote:
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);
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.