Guest User

Untitled

a guest
Nov 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. public class StringSearch {
  2. public static void main(String[] args) {
  3.  
  4. // Initialize a new String
  5. String myString = "Thanksgiving Day";
  6. System.out.println("Our string is: " + myString);
  7.  
  8. // Now, get the 3rd letter in this string.
  9. // Remember that indexes are zero based,
  10. // so the third character has index 2, not 3.
  11. char myThirdLetter = myString.charAt(2);
  12. System.out.println("The third letter is: " + myThirdLetter);
  13.  
  14. // Now we want to know the first location where
  15. // the letter 'g' occurs in our string.
  16. int myFirstG = myString.indexOf('g');
  17. System.out.println("The first time the letter g occurs is at location: " + myFirstG);
  18.  
  19. // If the character is not found, indexOf will
  20. // return -1
  21. int myFirstQ = myString.indexOf('q');
  22. System.out.println("The first time the letter q occurs is at location: " + myFirstQ);
  23.  
  24. // You can alse start your search at a specific
  25. // location in the string. Let's start at location 7.
  26. int mySecondG = myString.indexOf('g', 7);
  27. System.out.println("The first time the letter g occurs after location 6 is at location: " + mySecondG);
  28. }
  29. }
Add Comment
Please, Sign In to add comment