Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package strings;
- public class StringMethods {
- public static void main(String[] args) {
- // strings are immutable meaning you cannot change the contents after creating
- // what do we mean by this
- // show THE HEEP and how immutable objects are stored there until flushed (or never)
- // The main difference between heap and stack is that stack memory is used to store local variables and function call while
- // heap memory is used to store objects in Java.
- // strings can be one letter but use CHAR for this
- // class string is already provided by Jave so you don't need to make this yourself
- // show them how Ctrl Click on string will show the class designation code ON SOME COMPLIERS
- // NOT ours!
- // since Java 9 string is an array of Bytes (before it was an array of Char)
- // less space Char 8 bytes
- String s = " Elementary Elements ";
- String s2 = s.toLowerCase(); // elementary elements
- System.out.println("s2 ="+s2);
- // show them userinput. to show command options
- // just typing this String s9 = s. will give them the options
- // shows all this INSTANCE METHODS
- s = s.toUpperCase();
- System.out.println("s="+s);
- s2 = s2.trim(); // gets rid of the blanks before and after
- System.out.println("s2="+s2);
- int len = s2.length(); // lenght of the string
- System.out.println("len = "+len);
- // show them command first, change indesy for them
- // THENask them to build a for loop around it as inclass work
- // give them five minutes and then show them
- char indexy = s2.charAt(4);
- System.out.println(" char at 4 " + " = "+ indexy); // note indexes start at 0 for arrays
- for (int i=0; i < len ; i++) // walk through each indexy of the string
- {
- char c = s2.charAt(i);
- System.out.println(" char at "+ i + " ="+ c);
- }
- int count =0;
- for (int i=0; i < len ; i++) // Finds "e"
- {
- char c = s2.charAt(i);
- if (c == 'e')
- count += 1;
- }
- System.out.println("found " + count + " e's");
- String s3 = s2.substring(6); // substring at 6 shows the 6 to the end of string
- String s4 = s2.substring(6,10); // using range to just print out what we want
- System.out.println("s2.substring(6)="+ s3);
- System.out.println("s2.substring(6,10)="+ s4);
- int index = s2.indexOf('e'); // indexOf finds the first e's index number
- System.out.println("index ="+index);
- index = s2.indexOf('e' , index +1); // repeating it finds the next e's index number
- System.out.println("index ="+index);
- index = s2.indexOf('e' , index +1); // and the next the next e's index number
- System.out.println("index ="+index);
- index = s2.indexOf('e' , index +1); // and the next the next e's index number
- System.out.println("index ="+index);
- int start =0;
- int index2;
- int count2 =0;
- System.out.println("What is this?"); // this finds letters in string and tells you what index
- do
- {
- index2 = s2.indexOf('e', start);
- if ( index2 >= 0)
- {
- start = index2 +1;
- System.out.println("Found an e at " + index2);
- count2 += 1;
- }
- } while (index2 >= 0);
- System.out.println("Found " + count2 +" e's");
- start = 0;
- count2 = 0;
- do
- {
- index2 = s2.indexOf("men", start);
- if ( index2 >= 0)
- {
- start = index2 +1;
- System.out.println("Found men at " + index2);
- count2 += 1;
- }
- } while (index2 >= 0);
- System.out.println("Found " + count2 +" men's");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment