Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. public class Text2 {
  2.  
  3.     public static void main(String[] args){
  4.         //STRING METHODS
  5.         String s = "Brandon";
  6.         int x = s.length();
  7.         System.out.println(x); //s.length() is a method.  Don't forget the parantheses
  8.         char c = s.charAt(3); //remember this --> c gets assigned the value 'n'
  9.         System.out.println(c);
  10.  
  11.         //the format of these methods is string.methodName(parameters)
  12.         //so length has no parameters (that's why it's empty)
  13.         //String is any string object
  14.  
  15.         String s2 = s.concat(s); //this is equivalent to the + operation for Strings
  16.         System.out.println(s2);//BrandonBrandon
  17.  
  18.         //What if you wanted a "substring" of a String
  19.         String java = "pencil";
  20.         String javaSub = java.substring(0, 3); //this is a substring method with two parameter
  21.         //(beginning point, end point)
  22.         System.out.println(javaSub);
  23.  
  24.         //if you wanted the last characters from index 1 onwards
  25.         String javaChop = java.substring(1); //cuts the String from the index 1
  26.         //to the end.  So if substring uses one parameter in a code, you can know
  27.         //that it is equivalent to java.substring(1, java.length());
  28.         System.out.println(javaChop);
  29.  
  30.         //say you were working with some string of unknown length in some loop
  31.         String test = "ssfsa";
  32.         test = test.substring(test.length() - 2, test.length());
  33.         System.out.println(test);
  34.  
  35.         String cry = "cry";
  36.         char eraser = cry.charAt(3); //the 3 + 1 = 4th letter doesn't exist
  37.         System.out.println(cry.indexOf('c'));
  38.  
  39.         System.out.println(eraser);
  40.  
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement