473,385 Members | 2,210 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,385 software developers and data experts.

whole word substring search w/o using regular expressions

Is there a way to search a string for a whole word w/o using a regular
expression?

i.e.
mystring.IndexOf(mypattern, *WholeWord);

The reason i rather not use a regular expression is because sometimes i
want to search for whole words w/o the regular expression special
characters being interpreted.

Apr 20 '06 #1
4 4771
Why not simply escape the relevant characters in the word and then
build it into a regular expression? You could then wrap it all in a
method call:

int wordIndex = WordIndex(myString, wordString);

Apr 20 '06 #2
<se**********@yahoo.com> wrote:
Is there a way to search a string for a whole word w/o using a regular
expression?

i.e.
mystring.IndexOf(mypattern, *WholeWord);
Do you know what word separators will be involved? In other words, will
there always be spaces between words?
The reason i rather not use a regular expression is because sometimes i
want to search for whole words w/o the regular expression special
characters being interpreted.


You can use Regex.Escape to escape all the special characters, then use
the normal word boundary character classes etc. That sounds like the
best bet here, unless the word delimiter will always be the exact same
thing.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 20 '06 #3
spaces will be the word separators. thanks i guys, i'll look into the
escape method.

Apr 21 '06 #4
<se**********@yahoo.com> wrote:
spaces will be the word separators.


In that case, you could just use foo.IndexOf (" "+word+" ")

You could use foo.StartsWith (word+" "),
foo.EndsWith(" "+word) and
foo.Equals(word)

to check for edge cases.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 21 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Sandfordc | last post by:
I have tried several time to do this but have been unsucessful. I tried something like: myFunction(charater) str=frm.s1.value sb1=str.substring(0,charater)...
3
by: colinhumber | last post by:
How can I use regular expressions to search for a substring within another substring? For example, if I have the string: "This is * some text <button id="123">Some * text</button>" and I only...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
4
by: Amith Singh | last post by:
Hello All, I am using .Net Framework 1.1. Using Regular Expressions I would like to split a word(inserting a blank space) in a string if the word is more than N(say 50) characters. I can do the...
0
by: leeonions | last post by:
Hi there, i am trying to use regular expressions to search through a text string and replace a given whole word. take the string = "The matsat on the mat!" (bad example i know) i want to...
2
by: leeonions | last post by:
Hi there, i am trying to use regular expressions to search through a text string and replace a given whole word. take the string = "The matsat on the mat!" (bad example i know) i want to...
1
by: vmoreau | last post by:
I have a text and I need to find a Word that are not enclosed in paranthesis. Can it be done with a regex? Is someone could help me? I am not familar with regex... Example looking for WORD:...
3
by: balakrishnan.dinesh | last post by:
Hi frnds, Im having a problem in searching out a particular substring in a string. Here the code: --------------------- <html> <script> var tmpString=" ";
5
by: Chris Mahoney | last post by:
Hi I have a string containing a phrase, and I want to search for a particular word and get its index. I've tried a couple of things already: InStr(phrase, word) - this doesn't work because it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.