Strings [Java Code Snippets]

class Main {
 public static void main(String[] args) {
 
 // review of characters
 char a = '3';
 System.out.println(a);
 
 // declaring and assigning a string value
 String name = "John & ~";
 System.out.println(name);
 
 // prints out the number of characters in the string
 System.out.println(name.length());
 
 // prints out char at a specific index
 System.out.println(name.charAt(5));
 
 // compares two strings
 System.out.println(name.compareTo("Moche & ~"));
 
 // combines two strings
 System.out.println(name.concat(" Mocha"));
 
 // name contains Amadeus
 System.out.println(name.contains("AmadeusDoppleganger"));
 
 // name equals Amadeus (can't just use ==)
 System.out.println(name.equals("Amadeus"));
  // search the name for index (only put in a character)
 System.out.println(name.indexOf('t'));
 
 // prints a substring of a string
 System.out.println(name.substring(0,name.length()));
 
 }
}

No comments:

Post a Comment