User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 456,602 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,499 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 1365 | Replies: 3
Reply
Join Date: May 2006
Posts: 58
Reputation: kaushik259106 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
kaushik259106 kaushik259106 is offline Offline
Junior Poster in Training

Help Help regarding comparison of String in JS

  #1  
Sep 6th, 2007
How to do this type of comparison.
Say i have a variable with a word "ABCDEF"
and i want to check the combination "DEF" exists in it or not how do i do?
I want to make a simple comparison test..
Some friend suggested me the "substring" function but that works only in JSP i hope?

Please help me out!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 971
Reputation: MattEvans has a spectacular aura about MattEvans has a spectacular aura about 
Rep Power: 5
Solved Threads: 48
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is online now Online
Posting Shark

Re: Help regarding comparison of String in JS

  #2  
Sep 6th, 2007
Use the indexOf() method of the string object:

  1. var string_a = "ABCDEF";
  2. var string_b = "DEF";
  3. var iof_b = string_a.indexOf( string_b );
  4. if( iof_b == -1 )
  5. {
  6. alert( "Cannot find the string " + string_b + " in " + string_a );
  7. }
  8. else
  9. {
  10. alert( "Found the string " + string_b + " in " + string_a + " at character index " + iof_b );
  11. }
Last edited by MattEvans : Sep 6th, 2007 at 10:09 am.
Plato forgot the nullahedron..
Reply With Quote  
Join Date: Sep 2007
Posts: 69
Reputation: ExplainThat is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 7
ExplainThat ExplainThat is offline Offline
Junior Poster in Training

Re: Help regarding comparison of String in JS

  #3  
Sep 6th, 2007
You can certainly do what as Matt suggests. However, in the longer term if you plan to use JS a lot you would be better of learning to use regular expressions. Not necessary in this instance but the power they offer is well worth having under your belt. Here is the RegEx solution to your question

function Test()
 {
   var r = new RegExp("DEF");
   var s = "ABCDEF"
   document.write(r.test(s));
  }

This will write the string 'true' to your document. Naturally, you will want to do something else with the result.

Look here

http://www.regular-expressions.info

for an exhaustive discussion of regular expressions. Do note though that JS regular expressions are no way as powerful as in Perl etc.
Reply With Quote  
Join Date: May 2006
Posts: 58
Reputation: kaushik259106 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
kaushik259106 kaushik259106 is offline Offline
Junior Poster in Training

Re: Help regarding comparison of String in JS

  #4  
Sep 8th, 2007
lots of thanx for your help..
i used this method..
if(string1.search(string2)==-1)
    {
   // Not found
    }
    else
    {
  //  found
    }

see if that is usefull
cheers!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 7:07 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC