Advertisement
wingman007

Java_StringManipulation

Nov 23rd, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.09 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package stringmanipulation;
  8.  
  9. /**
  10.  *
  11.  * @author fmi
  12.  */
  13. public class StringManipulation {
  14.  
  15.     /**
  16.      * @param args the command line arguments
  17.      */
  18.     public static void main(String[] args) {
  19.         /*
  20.         // TODO code application logic here
  21.         String changeCase = "Text to Change";
  22.         System.out.println(changeCase);
  23.         // immutable we produce new strings we don't change the existing
  24.         String result;
  25.         result  = changeCase.toUpperCase();
  26.         System.out.println(result);
  27.  
  28.         System.out.println( result.toLowerCase( ) );  
  29.         */
  30.         /*
  31.         int result;
  32.         String word1 = "Ape";
  33.         String word2 = "App";
  34.        
  35.     result = word1.compareTo(word2);
  36.        
  37.         if (result < 0) {
  38.                 System.out.println("word1 is less than word2");
  39.         }
  40.         else if (result > 0) {
  41.                 System.out.println("word1 is more than word2");
  42.         }
  43.         else if ( result == 0) {
  44.                 System.out.println("the same words");
  45.         }
  46.         */
  47.         /*
  48.         // indexOf
  49.         // String ampersand = ".com"; // '@'; // or use a string
  50.         char ampersand = '@';
  51.         String email = "stoyan@hotmail.com";
  52.         int result = email.indexOf(ampersand);
  53.  
  54.         if (result == -1) {
  55.                 System.out.println("@ is missing in the string");
  56.         }
  57.         else {
  58.                 System.out.println("email is OK result = " + result);
  59.         }
  60.         // start position
  61.         int startPosition = 7;
  62.         result = email.indexOf(ampersand, startPosition);
  63.          System.out.println("start position result = " + result);
  64.        
  65.          
  66.         //
  67.         String dotcom = ".com";
  68.         Boolean ending = email.endsWith( dotcom );
  69.  
  70.         if (ending == false ) {
  71.         System.out.println( "Invalid Email Address" );
  72.         }
  73.         else {
  74.         System.out.println( "Email Address OK endsWith .com" );
  75.         }
  76.  
  77.         Boolean startVal = email.startsWith( dotcom );
  78.        
  79.         */
  80.         /*
  81.         // Substring
  82.         String FullName = "Bill Gates";
  83.         String FirstNameChars = "";
  84.         // This means start grabbing characters at position 0 in the string, eans the position in the string that you want to end at
  85.         FirstNameChars = FullName.substring( 0, 2 );
  86.  
  87.         System.out.println( FirstNameChars );
  88.        
  89.         // Now, Java will start at character two in the string FirstName, and then grab the characters from position 2 right to the end of the string.
  90.         String test = FullName.substring( 2 );
  91.         System.out.println( test );
  92.          */
  93.         /*
  94.         // !!!!!!    == !=  DON'T DO IT
  95.         // You can check two strings to see if they are the same. For this, use the equals method in Java.
  96.         String email1 = "stoyan@hotmail.com";
  97.         String email2 = "stoyan@hotmail.com";
  98.         Boolean isMatch = false;
  99.  
  100.         isMatch = email1.equals(email2);
  101.        
  102.         System.out.println( isMatch );
  103.         */
  104.         /*
  105.         String email = "meme@me.com";
  106.  
  107.         char aChar = email.charAt( 4 );
  108.         System.out.println( aChar );
  109.         */
  110.         /*
  111.         // replaces strings or chars
  112.     String aString = "Did you take you,books";
  113.     String correct = aString.replace("you", "your");
  114.     // aString.replace( '?', '@' )
  115.         System.out.println( correct );
  116.        
  117.        
  118.         String[] arrayString = aString.split("[ ,.]");
  119.         for (int i = 0; i < arrayString.length; i++) {
  120.             System.out.println(arrayString[i]);
  121.         }
  122.         */
  123.        
  124.         /*
  125.         String amend = " white space ";
  126.         System.out.println(amend);
  127.         amend = amend.trim( );
  128.         System.out.println(amend);
  129.         */
  130.        
  131.         String heading1 = "Exam_Name";
  132.         String heading2 = "Exam_Grade";
  133.  
  134.         System.out.printf( "%-15s %15s %n", heading1, heading2);
  135.     }
  136.    
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement