letsdoitjava

String Class Notes

Jun 19th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 KB | None | 0 0
  1. package strings;
  2.  
  3. public class StringMethods {
  4.  
  5.     public static void main(String[] args) {
  6.         // strings are immutable meaning you cannot change the contents after creating
  7.         //    what do we mean by this
  8.         //    show THE HEEP and how immutable objects are stored there until flushed (or never)
  9.         //      The main difference between heap and stack is that stack memory is used to store local variables and function call while
  10.         //      heap memory is used to store objects in Java.
  11.         // strings can be one letter but use CHAR for this
  12.         // class string is already provided by Jave so you don't need to make this yourself
  13.        
  14.         // show them how Ctrl Click on string will show the class designation code ON SOME COMPLIERS
  15.         // NOT ours!
  16.        
  17.         // since Java 9 string is an array of Bytes (before it was an array of Char)
  18.         // less space Char 8 bytes
  19.        
  20.         String s = "  Elementary Elements   ";
  21.  
  22.         String s2 = s.toLowerCase();  //  elementary elements
  23.         System.out.println("s2 ="+s2);
  24.        
  25.         // show them userinput. to show command options
  26.         // just typing this String s9 = s. will give them the options
  27.         // shows all this INSTANCE METHODS
  28.  
  29.         s = s.toUpperCase();
  30.         System.out.println("s="+s);
  31.  
  32.         s2 = s2.trim();                     // gets rid of the blanks before and after
  33.         System.out.println("s2="+s2);
  34.  
  35.         int len = s2.length();              // lenght of the string
  36.         System.out.println("len = "+len);
  37.  
  38. // show them command first, change indesy for them
  39. //        THENask them to build a for loop around it as inclass work
  40. // give them five minutes and then show them
  41.         char indexy = s2.charAt(4);
  42.         System.out.println(" char at 4 " + " = "+ indexy);  // note indexes start at 0 for arrays
  43.        
  44.         for (int i=0; i < len ;  i++)       // walk through each indexy of the string
  45.         {
  46.             char c = s2.charAt(i);
  47.             System.out.println(" char at "+ i + " ="+ c);
  48.         }
  49.  
  50.         int count =0;
  51.         for (int i=0; i < len ;  i++)       // Finds "e"
  52.         {
  53.             char c = s2.charAt(i);
  54.             if (c == 'e')
  55.                 count += 1;
  56.         }
  57.  
  58.         System.out.println("found " + count + " e's");
  59.  
  60.         String s3 = s2.substring(6);        // substring at 6 shows the 6 to the end of string
  61.         String s4 = s2.substring(6,10);     // using range to just print out what we want
  62.  
  63.         System.out.println("s2.substring(6)="+ s3);
  64.         System.out.println("s2.substring(6,10)="+ s4);
  65.  
  66.         int index = s2.indexOf('e');        // indexOf finds the first e's index number
  67.         System.out.println("index ="+index);
  68.  
  69.         index = s2.indexOf('e' , index +1); // repeating it finds the next e's index number
  70.         System.out.println("index ="+index);
  71.  
  72.  
  73.         index = s2.indexOf('e' , index +1); // and the next the next e's index number
  74.         System.out.println("index ="+index);
  75.  
  76.  
  77.         index = s2.indexOf('e' , index +1); // and the next the next e's index number
  78.         System.out.println("index ="+index);
  79.  
  80.  
  81.         int start =0;
  82.         int index2;
  83.         int count2 =0;
  84.  
  85.         System.out.println("What is this?");    // this finds letters in string and tells you what index
  86.         do
  87.         {
  88.             index2 = s2.indexOf('e', start);
  89.             if ( index2 >= 0)
  90.             {
  91.                 start = index2 +1;
  92.                 System.out.println("Found an e at " + index2);
  93.                 count2 += 1;
  94.             }
  95.  
  96.         } while (index2 >= 0);
  97.  
  98.         System.out.println("Found " + count2 +" e's");
  99.  
  100.         start = 0;
  101.         count2 = 0;
  102.         do
  103.         {
  104.             index2 = s2.indexOf("men", start);
  105.             if ( index2 >= 0)
  106.             {
  107.                 start = index2 +1;
  108.                 System.out.println("Found men  at " + index2);
  109.                 count2 += 1;
  110.             }
  111.  
  112.         } while (index2 >= 0);
  113.  
  114.         System.out.println("Found " + count2 +" men's");
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment