•
•
•
•
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 402,553 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 2,369 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: 1106 | Replies: 3
![]() |
•
•
Join Date: May 2006
Posts: 57
Reputation:
Rep Power: 3
Solved Threads: 0
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!
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!
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 943
Reputation:
Rep Power: 5
Solved Threads: 47
Use the indexOf() method of the string object:
Javascript Syntax (Toggle Plain Text)
var string_a = "ABCDEF"; var string_b = "DEF"; var iof_b = string_a.indexOf( string_b ); if( iof_b == -1 ) { alert( "Cannot find the string " + string_b + " in " + string_a ); } else { alert( "Found the string " + string_b + " in " + string_a + " at character index " + iof_b ); }
Last edited by MattEvans : Sep 6th, 2007 at 9:09 am.
If it only works in Internet Explorer; it doesn't work.
•
•
Join Date: Sep 2007
Posts: 67
Reputation:
Rep Power: 2
Solved Threads: 7
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
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.
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Pleeeeeeeese help me (JSP)
- Binary Search of an array of three strings (C)
- Help with String Comparison (C++)
- string comparison in VB.net (VB.NET)
- Applet Help (Java)
- sorting an array of string (C)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Loading Variables?
- Next Thread: ActiveXObject Problem


Linear Mode