Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Text2 {
- public static void main(String[] args){
- //STRING METHODS
- String s = "Brandon";
- int x = s.length();
- System.out.println(x); //s.length() is a method. Don't forget the parantheses
- char c = s.charAt(3); //remember this --> c gets assigned the value 'n'
- System.out.println(c);
- //the format of these methods is string.methodName(parameters)
- //so length has no parameters (that's why it's empty)
- //String is any string object
- String s2 = s.concat(s); //this is equivalent to the + operation for Strings
- System.out.println(s2);//BrandonBrandon
- //What if you wanted a "substring" of a String
- String java = "pencil";
- String javaSub = java.substring(0, 3); //this is a substring method with two parameter
- //(beginning point, end point)
- System.out.println(javaSub);
- //if you wanted the last characters from index 1 onwards
- String javaChop = java.substring(1); //cuts the String from the index 1
- //to the end. So if substring uses one parameter in a code, you can know
- //that it is equivalent to java.substring(1, java.length());
- System.out.println(javaChop);
- //say you were working with some string of unknown length in some loop
- String test = "ssfsa";
- test = test.substring(test.length() - 2, test.length());
- System.out.println(test);
- String cry = "cry";
- char eraser = cry.charAt(3); //the 3 + 1 = 4th letter doesn't exist
- System.out.println(cry.indexOf('c'));
- System.out.println(eraser);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement