1 / 9

Understanding Java String Manipulations: A Comprehensive Guide

This guide illustrates various operations on Java String objects using multiple classes. It covers concepts such as creating strings, checking for character occurrences, comparing strings, using methods like `contains`, `endsWith`, and `substring`, and converting strings to uppercase and lowercase. Practical examples are provided for manipulating strings effectively, which is essential for Java developers. By following this guide, you will enhance your understanding of string handling in Java, ensure robust code development, and improve your programming proficiency.

tyra
Download Presentation

Understanding Java String Manipulations: A Comprehensive Guide

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LAB - Strings CCIS - PNU

  2. Create String objects • String is a class. • String college = “ccis” ; • OR • String college = new String(“ccis”);

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

More Related