Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find string 1

Status
Not open for further replies.

Fursten

Programmer
Dec 27, 2000
403
PT
Hi,

I need to find a string in another string (with a lot of strings concatenated by a ";", except the last string witch could not have a ";"). Is the string is found, the function will return true.

str= test;so;last;comeback

If str_test = "test" then True

If str_teste = "come" it returns False (come is not a string it´s only part of a string)

Is there an easy way of doing this?


thank you
ps: I´m using jscript
 

I'd use this:

Code:
if ((string_to_test + ';').indexOf(string_to_test_for + ';') != -1) alert('found');

Hope this helps,
Dan

 
Dan's solution will work and is the easiest most direct way to do it. Depending on what you need to do afterwards you can also split the string into an array...

Code:
str= test;so;last;comeback
strArr = str.split(";")
str_test = "test"

for (x=0; x<strArr.length; x++){
  if(str_test == strArr[x]){
    return true
  }
}

Like I said - Dan's is much easier to implement

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
A little modification to Dan's code - this will match the search term exactly instead of any items ending with the term:
Code:
if ((';' + string_to_test + ';').indexOf(';' + string_to_test_for + ';') != -1) alert('found');
So using your example, this will match "test", but not "est".

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
or using regex:
Code:
var str = "test;so;last;comeback";
var str_test = "come";

var regex = "\\b" + str_test + "\\b"; // \b signifies a word boundary

if (str.match(regex))
{
  // do something
}
else
{
  // do something else
}

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
I was thinking of posting a regex solution, but I couldn't figure out what to do if the string to test for contained special characters. I haven't come up with anything. Any ideas on what to do about that?

Adam
"Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done." Andy Rooney.
 
Modify the string?
Code:
var str_to_test = "co@me";

var chars = str_to_test.split("");
for (var i=0; i<chars.length; i++)
{
  if (chars[i].match("\\W"))
    chars[i] = "\\b" + chars[i] + "\\b";
}
str_to_test = chars.join("");
Result: co\b@\bme
"a;b;co@me;c".match("\\b" + str_to_test + "\\b") = "co@me"
"a;b;co@me[red]a[/red];c".match("\\b"+str_to_test+"\\b") = null

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top