473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

String issues

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
7 2049
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Java script Dude | last post by:
In many languages, it is necessary to string together multiple strings into one string for use over multiple lines of code. Which one is the most efficient from the interpreters perspective: ...
4
by: gamehack | last post by:
Hi all, I've been designing a program which would need to have a function to replace values in a string. For example I could have a string: "<circle x="%" y="%">" and then I would replace the %...
1
by: John Fly | last post by:
I've used standard functions like find_first_of and find_first_not_of for some time. I was about to implement a quick string tokenizer similar to the example below when I ran across an old thread...
8
by: tbh | last post by:
for historical reasons i need to be able to call, from C# under DotNet 2, as COM+ DLL function that returns a "string" which is really an array of seemingly arbitrary bytes (presumably non-zero)....
11
by: Lothar Behrens | last post by:
Hi, I have selected strtok to be used in my string replacement function. But I lost the last token, if there is one. This string would be replaced select "name", "vorname", "userid",...
4
by: Peter Larsen [] | last post by:
Hi, How do i read a resource string from a dll in code ?? BR Peter
15
by: Cartoper | last post by:
There is one little static C library to manage the serial number and unlock key for my application. Today it is compiled with Microsoft VC6 and linked into both VC6 modules and VS2005 modules. It...
3
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I have an xsd that I want to save as an XML string to store in a DB I can save as a physical file using xsd.WriteXml(@"C:\Temp\Junk\junk.xml"); But I am unable to save to a string so I...
2
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi Is there any way to Read an INIFile from a string or Stream instead of a physical file ??? I want to read the INIFile into a string then store in a db but when I read the string from the...
8
by: Brett | last post by:
I wrote an ASP.NET application that queries a SQL Server database (on a different box from the web server) and displays the result in a GridView. The datasource for the GridView is a SQLDataSource....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.