JAVA – String Function PROF. S. LAKSHMANAN,

Slides:



Advertisements
Similar presentations
Java
Advertisements

1 StringBuffer & StringTokenizer Classes Chapter 5 - Other String Classes.
Strings in Java 1. strings in java are handled by two classes String &
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Working with String Duo Wei CS110A_ Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Programming 2 CS112- Lab 2 Java
Chapter 7: The String class We’ll go through some of this quickly!
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
23-Jun-15 Strings, Etc. Part I: String s. 2 About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects,
StringBuffer class  Alternative to String class  Can be used wherever a string is used  More flexible than String  Has three constructors and more.
Fundamental Programming Structures in Java: Strings.
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
String StringBuffer. class StringExample { public static void main (String[] args) { String str1 = "Seize the day"; String str2 = new String(); String.
28-Jun-15 String and StringBuilder Part I: String.
The String Class. Objectives: Learn about literal strings Learn about String constructors Learn about commonly used methods Understand immutability of.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic.
Class T{ public static int id; public static int num = 5; public int n; public boolean c; public static void setNumberOfBicycles(int val) { num=val; //
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
String Class. Objectives and Goals Identify 2 types of Strings: Literal & Symbolic. Learn about String constructors and commonly used methods Learn several.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;
String Handling StringBuffer class character class StringTokenizer class.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
1 The String Class F Constructing a String: F Obtaining String length and Retrieving Individual Characters in a string F String Concatenation (concat)
Strings JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Strings Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and.
String Definition A String is a set of characters that behaves as a single unit. The characters in a String include upper-case and lower-case letters,
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes.
CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings.
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
17-Feb-16 String and StringBuilder Part I: String.
Chapter 8 String Manipulation
The String class is defined in the java.lang package
String and StringBuffer classes
Strings.
EKT 472: Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
Programming in Java Text Books :
String and String Buffers
String Class.
Strings Chapter 6.
String Handling in JAVA
String class in java Visit for more Learning Resources string.
Modern Programming Tools And Techniques-I Lecture 11: String Handling
String and StringBuilder
F4105 JAVA PROGRAMMING F4105 Java Programming
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
String and StringBuilder
String and StringBuilder
Java – String Handling.
Lecture 07 String Jaeki Song.
String and StringBuilder
String methods 26-Apr-19.
Strings in Java.
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
In Java, strings are objects that belong to class java.lang.String .
Presentation transcript:

JAVA – String Function PROF. S. LAKSHMANAN, DEPT. OF B. VOC. (SD & SA), ST. JOSEPH'S COLLEGE.

String Strings, which are widely used in Java programming, are a sequence of characters Once a String object is created it cannot be changed. Stings are Immutable. To get changeable strings use the class called StringBuffer. String and StringBuffer classes are declared final, so there cannot be subclasses of these classes. The default constructor creates an empty string. String s = new String();

Creating Strings The most direct way to create a string is to write: String greeting = "Hello world!"; Eg: char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' }; String helloString = new String(helloArray); System.out.println(helloString);

Creation of StringBuffer we have used the following StringBuffer Constructors. StringBuffer() //Default Constructor with 16 characters set StringBuffer(String s) //String to String Buffer StringBuffer(int initialCapacity) //StringBuffer?s with initial Capacity 

Example for Stringbuffer public class StringBufferExample {   public static void main(String[] args)  {   StringBuffer strBuffer1 = new StringBuffer("Bonjour");     StringBuffer strBuffer2 = new StringBuffer(60);     StringBuffer strBuffer3 = new StringBuffer();        System.out.println("strBuffer1 : " +strBuffer1);   System.out.println("strBuffer2 capacity : " +strBuffer2.capacity());   System.out.println("strBuffer3 capacity : " +strBuffer3.capacity());   } }

Compare (== operator) public class stringmethod {   public static void main(String[] args) {   String string1 = "Hi";   String string2 = new String("Hello");   if (string1 == string2)  {   System.out.println("The strings are equal.");   } else  {   System.out.println("The strings are unequal.");   }   } }  

Concatenating Strings public class StringDemo { public static void main(String args[]) String string1 = "saw I was "; System.out.println("Dot " + string1 + "Tod"); }

String Handlings lastIndexOf(String str); charAt(index); equals(obj) compareTo(anotherString); compareToIgnoreCase(String str); concat(String str); endsWith(String suffix); toLowerCase(), toUpperCase() equalsIgnoreCase(String anotherString); indexOf(String str); indexOf(int ch);, trim() indexOf(String str,int fromindex); toCharArray(); indexOf(int ch,int fromindex); lastIndexOf(String str); lastIndexOf(int ch); lastIndexOf(String str,int fromindex); lastIndexOf(int ch,int fromindex); length(); replace(oldChar, newChar) replaceAll(String old, String new) replaceFirst(String old, String new) split(str) startsWith(String prefix); substring(beginIndex) substring(beginIndex, endIndex)

Methods String Length public class StringDemo { public static void main(String args[]) String palindrome = "Dot saw I was Tod"; int len = palindrome.length(); System.out.println( "String Length is : " + len ); }

charAt() public class Test { public static void main(String args[]) String s = "Strings are immutable"; char result = s.charAt(8); System.out.println(result); }

compareTo() public class Test { public static void main(String args[]) String str1 = "Strings are immutable"; String str2 = "Strings are immutable"; String str3 = "Integers are not immutable"; int result = str1.compareTo( str2 ); System.out.println(result); result = str2.compareTo( str3 ); System.out.println(result); result = str3.compareTo( str1 ); System.out.println(result); }

compareToIgnoreCase() public class Test { public static void main(String args[]) String str1 = "Strings are immutable"; String str2 = "Strings are immutable"; String str3 = "Integers are not immutable"; int result = str1.compareToIgnoreCase( str2 ); System.out.println(result); result = str2.compareToIgnoreCase( str3 ); System.out.println(result); result = str3.compareToIgnoreCase( str1 ); System.out.println(result); }

concat() public class Test { public static void main(String args[]) String s = "Strings are immutable"; s = s.concat(" all the time"); System.out.println(s); }

Methods — Equality boolean b = word1.equals(word2); returns true if the string word1 is equal to word2 boolean b = word1.equalsIgnoreCase(word2); returns true if the string word1 matches word2, case-blind b = “Raiders”.equals(“Raiders”);//true b = “Raiders”.equals(“raiders”);//false b = “Raiders”.equalsIgnoreCase(“raiders”);//true if(team.equalsIgnoreCase(“raiders”)) System.out.println(“Go You “ + team);

trim() import java.io.*; public class Test { public static void main(String args[]) String Str = new String(" Welcome to Capella "); System.out.print("Return Value :" ); System.out.println(Str.trim() ); }

substring(int beginIndex) public class StrSearch{   public static void main(String args[]) {   String s = "Bonjour";   //String begin = 1;   String t = s.substring(1);    System.out.println(t);   } } 

substring(int beginIndex, int endIndex) public class StringSearch{   public static void main(String args[]){   String s = "Bonjour";   //String begin = 3;   //String end = 7;   String t = s.substring(3, 7);    System.out.println(t);   }   }  

Methods — Find (indexOf) 0 2 6 10 15 String name =“President George Washington"; name.indexOf (‘P'); 0 name.indexOf (‘e'); 2 name.indexOf (“George"); 10 name.indexOf (‘e', 3); 6 name.indexOf (“Bob"); -1 name.lastIndexOf (‘e'); 15 Returns: (starts searching at position 3) (not found)

indexOf(int ch) public class indexOfString {   public static void main(String args[])  {   String s = "That was the breaking News";   System.out.println(s);  System.out.println("indexOf(the)  " + s.indexOf('b'));    }   }

indexOf(int ch, int fromIndex) public class IndexOfChar {   public static void main(String args[])  {   String s = "Honesty is the best Policy";   System.out.println(s);   System.out.println("indexOf(n, 1) -> " + s.indexOf('b', 1));   System.out.println("indexOf(n, 1) -> " + s.indexOf(98, 1));   }   }

indexOf(String str, int fromIndex) public class IndexOfStr {   public static void main(String args[])  {   String s = "That was the breaking News";   System.out.println(s);   System.out.println("indexOf(News, 5) -> " + s.indexOf("News", 5));   }   }

lastIndexOf(int ch) public class LastIndexOfCh {   public static void main(String args[]) {   String s = "Honesty is the best Policy";   System.out.println(s);     System.out.println("lastIndexOf(b) " + s.lastIndexOf('b'));   }   }

lastIndexOf(String str) public class LastIndxOfStr  {   public static void main(String args[])  {   String s = "Honesty is the best Policy";   System.out.println(s);   System.out.println("lastIndexOf(the) -> " + s.lastIndexOf("nest"));   }   }

lastIndexOf(String str, int fromIndex) public class lastindexofStrInd  {   public static void main(String args[])  {   String s = "Java is a wonderful language";   System.out.println(s);   System.out.println("lastIndexOf(ful, 18) = " + s.lastIndexOf("ful", 18));   }   }

replaceFirst() Method public class ReplaceFirstDemo  {   public static void main(String[] args)  {   String str = "Her name is jeni and jeni is a good girl.";   String strreplace = “kanika";   String result = str.replaceFirst(“jeni", strreplace);   System.out.println(result);   } }

replaceAll() public class ReplaceDemo  {   public static void main(String[] args)  {   String str = "Her name is jenifer and  jenifer  is a good girl.";   String strreplace = "Sonia";   String result = str.replaceAll(“jenifer ", strreplace);   System.out.println(result);   } }

toLowerCase and toUpperCase public class MainClass {    public static void main( String args[] )    {       String s1 = new String( "hello" );       String s2 = new String( "GOODBYE" );       String s3 = new String( "   spaces   " );       System.out.printf( "s1 = \ns2 = \ns3 = \n\n", s1, s2, s3 );       // test toLowerCase and toUpperCase       System.out.printf( "s1.toUpperCase() = %s\n", s1.toUpperCase() );       System.out.printf( "s2.toLowerCase() = %s\n\n", s2.toLowerCase() );    } }

reverse() public class StringReverseExample {     public static void main(String args[]){          String strOriginal = "Hello World";     System.out.println("Original String : " + strOriginal);              strOriginal = new StringBuffer(strOriginal).reverse();             System.out.println("Reversed String :" + strOriginal);   } }

Numbers to Strings Three ways to convert a number into a string: 1. String s = "" + num; 2. String s = Integer.toString (i); String s = Double.toString (d); 3. String s = String.valueOf (num); s = “” + 123;//”123” Integer and Double are “wrapper” classes from java.lang that represent numbers as objects. They also provide useful static methods. s = Integer.toString(123);//”123” s = Double.toString(3.14); //”3.14” s = String.valueOf(123);//”123”

To remove a character public class Main {   public static void main(String args[]) {     String str = "this is a test";          System.out.println(removeChar(str,'s'));   }   public static String removeChar(String s, char c) {     String r = "";     for (int i = 0; i < s.length(); i++) {       if (s.charAt(i) != c)         r += s.charAt(i);     }     return r;   } }

Start with  import java.lang.*; public class StrStartWith{   public static void main(String[] args) {   System.out.println("String start with example!");   String str = "Welcome to Capella";   String start = "Wel";   System.out.println("Given String : " + str);   System.out.println("Start with : " + start);   if (str.startsWith(start)){   System.out.println("The given string is start with Wel");   }   else{   System.out.println("The given string is not start with Wel");   }   } }

End with import java.lang.*; public class StrEndWith{   public static void main(String[] args) {   System.out.println("String end with example!");   String str = "Welcome to RoseIndia";   String end = "RoseIndia";   System.out.println("Given String : " + str);   System.out.println("End with : " + end);   if (str.endsWith(end)){   System.out.println("The given string is end with RoseIndia");   }   else{   System.out.println("The given string is not end with RoseIndia");   }   } }

split string class StringSplitExample {         public static void main(String[] args) {                 String st = "Hello_World";                 String str[] = st.split("_");                 for (int i = 0; i < str.length; i++) {                         System.out.println(str[i]);                 }         } }

Making Tokens of a String Tokens can be used where we want to break an application into tokens. StringTokenizer is a class in java.util.package.  Methods in string tokenizer countTokens(): It gives the number of tokens remaining in the string. hasMoreTokens(): It gives true if more tokens are available, else false. nextToken(): It gives the next token available in the string.

Thank You.