Author |
Message |
Morice
|
Posted: Thu Mar 30, 2006 3:49 pm Post subject: Index in Java help |
|
|
Is there an index function in Java script like the one in Turing tat i can use for a hangman game? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
wtd
|
Posted: Thu Mar 30, 2006 3:59 pm Post subject: (No subject) |
|
|
Javascript is not Java.
Also, for those not familiar with Turing's "index" function, please provide a description of what you wish to accomplish. |
|
|
|
|
![](images/spacer.gif) |
Morice
|
Posted: Thu Mar 30, 2006 4:02 pm Post subject: (No subject) |
|
|
ok. I want to see if in a word is ther a letter tat im searching for. |
|
|
|
|
![](images/spacer.gif) |
Corvillus
|
Posted: Thu Mar 30, 2006 5:52 pm Post subject: (No subject) |
|
|
I'd guess you're looking for the String.indexOf() method, which returns the index of the first occurence of either a given character or a given string within a string, or -1 for no occurence. For example:
code: |
String s = "Hello, world!";
System.out.println(s.indexOf('o')); //outputs 4
|
|
|
|
|
|
![](images/spacer.gif) |
[Gandalf]
![](http://compsci.ca/v3/uploads/user_avatars/189297994e4c716fec7f1.png)
|
Posted: Thu Mar 30, 2006 7:48 pm Post subject: (No subject) |
|
|
Or if you're talking about JavaScript...
JavaScript: | var str="Java is not JavaScript"
document.write(str.indexOf("Java")) |
The method works the exact same way as the Java example posted by Corvillus. |
|
|
|
|
![](images/spacer.gif) |
|