String CSI 3125 Preliminaries page 1 String Class

  • Slides: 20
Download presentation
String CSI 3125, Preliminaries, page 1

String CSI 3125, Preliminaries, page 1

String Class • Java provides the String class to create and manipulate strings. •

String Class • Java provides the String class to create and manipulate strings. • Creating Strings: • The most direct way to create a string is • String greeting = "Hello world!"; CSI 3125, Preliminaries, page 2

String Class Constructor • The String class supports several constructors. • To create an

String Class Constructor • The String class supports several constructors. • To create an empty String, call the default constructor. • String s = new String(); CSI 3125, Preliminaries, page 3

String Class Parameterized Constructor (1) • To create a String initialized by an array

String Class Parameterized Constructor (1) • To create a String initialized by an array of characters, use the constructor • public class String. Demo{ • public static void main(String args[]){ • char[] hello. Array = { ‘p', ‘o', ‘p', ‘o'}; • String S = new String(hello. Array); • System. out. println(S ); } } • This constructor initializes S with the string “popo”. CSI 3125, Preliminaries, page 4

String Class Parameterized Constructor (2) • Can specify a subrange of a character array

String Class Parameterized Constructor (2) • Can specify a subrange of a character array as an initializer using the following constructor: • String(chars[ ], int start. Index, int num. Chars) • start. Index specifies the index at which the subrange begins, and • num. Chars specifies the number of characters to use. • Here is an example: • chars[] = { 'a', 'b', 'c', 'd', 'e', 'f' }; • String s = new String(chars, 2, 3); • This initializes s with the characters cde. CSI 3125, Preliminaries, page 5

String Class Parameterized Constructor (3) • Copy one String object to another • Using

String Class Parameterized Constructor (3) • Copy one String object to another • Using the following constructor • String(String str. Obj) - str. Obj is a String object. • • class Make. String { public static void main(String args[]) { char c[] = {'J', 'a', 'v', 'a'}; String s 1 = new String(c); String s 2 = new String(s 1); System. out. println(s 2); }} CSI 3125, Preliminaries, page 6

String Class Parameterized Constructor (4) • Initializing string with ascii characters • • String(byte

String Class Parameterized Constructor (4) • Initializing string with ascii characters • • String(byte ascii. Chars[ ]) String(byte ascii. Chars[ ], int start. Index, int num. Chars) • • • class Sub. String. Cons { public static void main(String args[]) { byte ascii[] = {65, 66, 67, 68, 69, 70 }; String s 1 = new String(ascii); System. out. println(s 1); String s 2 = new String(ascii, 2, 3); System. out. println(s 2); }} o/p ABCDEF CDE CSI 3125, Preliminaries, page 7

String Length • The length() method, which returns the number of characters contained in

String Length • The length() method, which returns the number of characters contained in the string object. • • chars[] = { 'a', 'b', 'c' }; String s = new String(chars); System. out. println(s. length()); 3 • • String palindrome = "Dot saw I was Tod"; int len = palindrome. length(); System. out. println( "String Length is : " + len ); 7 CSI 3125, Preliminaries, page 8

String equals • compares the two given strings based on the content of the

String equals • compares the two given strings based on the content of the string. If any character is not matched, it returns false. If all characters are matched, it returns true. • eg • String s 1="javatpoint"; • String s 2="JAVATPOINT"; • System. out. println(s 1. equals(s 2)); //false because case is not same CSI 3125, Preliminaries, page 9

String equals. Ignore. Case • To perform a comparison that ignores case differences, call

String equals. Ignore. Case • To perform a comparison that ignores case differences, call equals. Ignore. Case( ). • When it compares two strings, it considers A-Z to be the same as a-z • String s 1="javatpoint"; • String s 2="JAVATPOINT"; • System. out. println(s 1. equals. Ignore. Case(s 2)); //true CSI 3125, Preliminaries, page 10

String Compare • • • compare. To( ) It returns integer value eg Str

String Compare • • • compare. To( ) It returns integer value eg Str 1. compare. To(str 2); Str 1 & str 2 are String Object • Less than zero The invoking string is less than str 2. • Greater than zero The invoking string is greater than str 2. • Zero The two strings are equal. CSI 3125, Preliminaries, page 11

String Concatenation • string 1. concat(string 2); • This returns a new string that

String Concatenation • string 1. concat(string 2); • This returns a new string that is string 1 with string 2 added to it at the end. • "My name is ". concat(“POPO"); • commonly concatenated with the + operator, as • "Hello, " + " world" + "!" CSI 3125, Preliminaries, page 12

String Search • The String class provides two methods that allow to search a

String Search • The String class provides two methods that allow to search a string for a specified character or substring: • ■ index. Of( ) Searches for the first occurrence of a character or substring. • ■ last. Index. Of( ) Searches for the last occurrence of a character or substring. • Eg • Strint s=“ajith”; • s. last. Index. Of('t') 3 CSI 3125, Preliminaries, page 13

String substring( ) extract a substring using substring( ). It has two forms String

String substring( ) extract a substring using substring( ). It has two forms String substring(int start. Index) start. Index specifies the index at which the substring will begin. • This form returns a copy of the substring that begins at start. Index and runs to the end of the invoking string. • The second form of substring( ) allows to specify both the beginning and ending index of the substring: • String substring(int start. Index, int end. Index) • • CSI 3125, Preliminaries, page 14

String contains • searches the sequence of characters in this string. It returns true

String contains • searches the sequence of characters in this string. It returns true if sequence of char values are found in this string otherwise returns false. • eg • String name="what do you know about me"; • System. out. println(name. contains("do you know")); • o/p true CSI 3125, Preliminaries, page 15

String replace • replace( ) • The replace( ) method replaces all occurrences of

String replace • replace( ) • The replace( ) method replaces all occurrences of one character in the invoking string with another character. • String replace(char original, char replacement) • String s = "Hello“; • S. . replace('l', 'w'); • puts the string “Hewwo” into s. CSI 3125, Preliminaries, page 16

String trim() • The trim( ) method returns a copy of the invoking string

String trim() • The trim( ) method returns a copy of the invoking string from which any leading and trailing whitespace has been removed. • String s=“ hello “; • S. trim(); “hello” CSI 3125, Preliminaries, page 17

String format • • • • method returns the formatted string by given format

String format • • • • method returns the formatted string by given format and arguments. Eg String name=“popo"; String sf 1=String. format("name is %s", name); String sf 2=String. format("value is %f", 32. 33434); String sf 3=String. format("value is %32. 12 f", 32. 33434); //returns 12 char fra ctional part filling with 0 System. out. println(sf 1); System. out. println(sf 2); System. out. println(sf 3); o/p name is spopo value is 32. 334340000000 CSI 3125, Preliminaries, page 18

String change case • String to. Lower. Case( ) • String to. Upper. Case(

String change case • String to. Lower. Case( ) • String to. Upper. Case( ) CSI 3125, Preliminaries, page 19

String CSI 3125, Preliminaries, page 20

String CSI 3125, Preliminaries, page 20