import java.util.Scanner; /** Some more examples for String processing. */ public class StringModified { /** These methods return a copy of the string that is modified. * concat * replace -- replace a character with another character * trim -- gets rid of whitespace in the beg and end of string * substring (returns a part of the string.) * NONE OF THESE METHODS ACTUALLY MODIFY THE STRING THAT THEY ARE CALLED ON SINCE STRINGS IN JAVA ARE IMMUTABLE (NONCHANGEABLE) */ public static void main(String[] args) { String doc="Four score and seven years ago"; String sub=doc.substring(5,10); System.out.println("sub returned from substring is: "+sub); // doc=doc.substring(5,10); //this is legal. We are saying that the // reference variable doc should refer to a different String object //System.out.println("doc is now: "+doc); // calling sample final question System.out.println("grunf returned: " + grunf("Hello","POPE","Ze")); Scanner in = new Scanner(System.in); String answer; System.out.print("do you want to continue?"); answer = in.nextLine(); String newans=answer.trim(); if (newans.charAt(0)=='Y' || newans.charAt(0)=='y') System.out.println("continuing..."); if (answer.trim().charAt(0)=='y') System.out.println("continuing..."); System.out.println("doc with replaced e is: " + doc.replace('e', '*')); // concatenating 2 strings means connecting them into one string String lastName="Doe"; String firstName="John"; String fullName=lastName.concat(firstName); // Problem: Given a pattern, you want to test whether this // pattern occurs in tandem anywhere in your text // example: AAG occurs as AAGAAG // Solution 1: you should solve using a loop, finding pattern and checking // if it is followed by another pattern. // Solution 2: String text="TTAAGAAGCC"; String pattern="AAG"; if (text.indexOf(pattern.concat(pattern))!=1) System.out.println("tandem repeat found!"); } /** grunf is a question on the sample final */ public static String grunf(String s1, String s2, String s3) { String s = s1.substring(1,2); if (s1.length()>s2.length() && s2.compareTo(s3)<0 && s3.indexOf(s)>0) return s1.substring(0,1) + s2.substring(1); else return s1.substring(0,1) + s2.substring(0,1) + s1.substring(0,1); } }