90 likes | 210 Views
LAB - Strings. CCIS - PNU. Create String objects. S tring is a class. String college = “ ccis †; OR String college = new String(“ ccis â€);. package stringsTestPackage ; import java.util.Scanner ; public class StringsTest { public static void main(String[] args ) {
E N D
LAB - Strings CCIS - PNU
Create String objects • String is a class. • String college = “ccis” ; • OR • String college = new String(“ccis”);
packagestringsTestPackage; • importjava.util.Scanner; • publicclassStringsTest { • publicstaticvoid main(String[] args) { • Scanner input = new Scanner(System.in); • String s1 = new String("JAVA"); • String s2 = "The best prctise, the best results performance"; • String s3 = new String("Java is easy, I love Java"); • for(int pos=0; pos < s1.length(); pos++) • System.out.println("Char at Position "+pos+" is "+s1.charAt(pos)); • System.out.println("************************"); • String traget = "performance"; • if(s2.contains(traget)) // OR if(s2.contains("performance")) • System.out.println("s2 contains "+traget); • else • System.out.println("s2 doesn't contain "+traget); • System.out.println("************************"); • String t = "C++"; • if(s3.endsWith(t)) // OR if(s3.endsWith("Java")) • System.out.println("s3 endsWith "+t); • else • System.out.println("s3 doesn't endsWith "+t); • input.close(); • }// end method main • }// end class StringsTest
package stringsTest2Package; • publicclass StringsTest2 { • publicstaticvoid main(String[] args) { • String s1 = "java"; • String s2 = new String("JAVA"); • if(s1.equals(s2)) • System.out.println(s1+" equals "+s2); • else • System.out.println(s1+" NOT equal "+s2); • if(s1.equalsIgnoreCase(s2)) • System.out.println(s1+" equals "+s2); • else • System.out.println(s1+" NOT equal "+s2); • }// end method main • }// end class StringsTest2
package stringsTest3; • publicclass StringsTest3 { • publicstaticvoid main(String[] args) { • String s = "The best practise, the best results"; • String target = "best"; • intfirstOccur = s.indexOf(target); //OR intfirstOccur = s.indexOf("best"); • System.out.println("of the first occurrence of "+target+" is "+firstOccur); • intlastOccur = s.lastIndexOf(target); //OR intlastOccur = s.indexOf("best"); • System.out.println("of the last occurrence of "+target+" is "+lastOccur); • }//end method main • }// end class StringsTest3
package stringsTest4Package; publicclass StringsTest4 { publicstaticvoid main(String[] args) { String s = "The best practise, the best results"; String subStr = s.substring(19); String subStr2 = s.substring(9, 16); System.out.println("s = "+s); System.out.println("substring from position 19 = "+subStr); System.out.println("substring from position 9 to 16 = "+subStr2);//you expect practise! }// end method main }// end class StringsTest4
package stringsTest5Package; publicclass StringsTest5 { publicstaticvoid main(String[] args) { String a = "CCIS PNU"; String b = "advanced java"; String aLower = a.toLowerCase(); String bUpper = b.toUpperCase(); System.out.println(a+" in lowercase = "+aLower); System.out.println(b+" in uppercase = "+bUpper); System.out.println("******************************************"); String x = "I am StuDenT"; String xLower = x.toLowerCase(); String xUpper = x.toUpperCase(); System.out.println(x+" in lowercase = "+xLower); System.out.println(x+" in uppercase = "+xUpper); }//end method main }//end class StringsTest5