Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings.

Similar presentations


Presentation on theme: "CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings."— Presentation transcript:

1 CSI 3125, Preliminaries, page 1 String

2 CSI 3125, Preliminaries, page 2 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!";

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

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

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

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

7 CSI 3125, Preliminaries, page 7 String Class Parameterized Constructor (4) Initializing string with ascii characters String(byte asciiChars[ ]) String(byte asciiChars[ ], int startIndex, int numChars) class SubStringCons { public static void main(String args[]) { byte ascii[] = {65, 66, 67, 68, 69, 70 }; String s1 = new String(ascii); System.out.println(s1); String s2 = new String(ascii, 2, 3); System.out.println(s2); }} o/p ABCDEF CDE

8 CSI 3125, Preliminaries, page 8 String Length The length() method, which returns the number of characters contained in the string object. char 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

9 CSI 3125, Preliminaries, page 9 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 s1="javatpoint"; String s2="JAVATPOINT"; System.out.println(s1.equals(s2 ));//false because case is not same

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

11 CSI 3125, Preliminaries, page 11 String Compare compareTo( ) It returns integer value eg Str1.compareTo(str2); Str1 & str2 are String Object Less than zero The invoking string is less than str2. Greater than zero The invoking string is greater than str2. Zero The two strings are equal.

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

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

14 CSI 3125, Preliminaries, page 14 String substring( ) extract a substring using substring( ). It has two forms String substring(int startIndex) startIndex specifies the index at which the substring will begin. This form returns a copy of the substring that begins at startIndex 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 startIndex, int endIndex)

15 CSI 3125, Preliminaries, page 15 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

16 CSI 3125, Preliminaries, page 16 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.

17 CSI 3125, Preliminaries, page 17 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”

18 CSI 3125, Preliminaries, page 18 String format method returns the formatted string by given format and arguments. Eg String name=“popo"; String sf1=String.format("name is %s",name); String sf2=String.format("value is %f",32.33434); String sf3=String.format("value is %32.12f",32.33434);//returns 12 char fra ctional part filling with 0 System.out.println(sf1); System.out.println(sf2); System.out.println(sf3); o/p name is spopo value is 32.334340 value is 32.334340000000

19 CSI 3125, Preliminaries, page 19 String change case String toLowerCase( ) String toUpperCase( )

20 CSI 3125, Preliminaries, page 20 String


Download ppt "CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings."

Similar presentations


Ads by Google