| Author |
Finding exact element in a javascript variable
|
pramod talekar
Ranch Hand
Joined: Apr 26, 2010
Posts: 316
|
|
Hello,
I need to find an exact element dynamically from a javascript variable.
In the present case, indexOf() method is used.
The problem is that if my variable contains a value like SD06 & SD06(A) e.g. cdSD06xxSD06(A), and if I keep a while loop condition with method,
then, the code in the loop will be executed twice as it contains SD06 twice though I want to execute it just once as SD06 & SD06(A) are completely different from each other.
Please advise.
|
Thanks,
Pramod
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56214
|
|
|
If you don't want a loop, why have you used a loop?
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
pramod talekar
Ranch Hand
Joined: Apr 26, 2010
Posts: 316
|
|
I do want to use a loop.
just need a replacement for indexOf() method.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56214
|
|
|
If you only want to find the first occurrence, you don't need the loop. If you don't want to just find the first occurrence, then you need to explain what you want better.
|
 |
pramod talekar
Ranch Hand
Joined: Apr 26, 2010
Posts: 316
|
|
Right.
For the While loop, there should be no loop at all, i.e. it shouldn't exist.
What if they change it to the IF loop?
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56214
|
|
Answer the following question with one of: true or false.
I only want to find the first occurrence of the substring.
If you answered true, then there is no need for any sort of loop. And, by the way, an if statement is not considered a "loop". If the intent is to conditionally execute code if the substring is found, then yes, an if statement is appropriate.
|
 |
pramod talekar
Ranch Hand
Joined: Apr 26, 2010
Posts: 316
|
|
It seems that I do need to use If loop.
sorry for the confusion.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
|
The "if" statement is not a loop. There is no such thing as an "if loop".
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
pramod talekar
Ranch Hand
Joined: Apr 26, 2010
Posts: 316
|
|
Yeah, right.
Anyways, I figured out the solution for my problem.
Thanks everyone for the advice.
Cheers..
|
 |
 |
|
|
subject: Finding exact element in a javascript variable
|
|
|