Presentation is loading. Please wait.

Presentation is loading. Please wait.

In Java, strings are objects that belong to class java.lang.String .

Similar presentations


Presentation on theme: "In Java, strings are objects that belong to class java.lang.String ."— Presentation transcript:

1

2 In Java, strings are objects that belong to class java.lang.String .
String class objects work with complete strings. Instead of treating them as character arrays as some languages do Strings are immutable. Whenever it looks as if a String object was modified, a new String was actually created and the old one was thrown away.

3 As it is a common class, Java permits you to skip the package name, even without a corresponding import statement. For example, String username; is equivalent to java.lang.String username;

4 String (string astring)
public String() : A new empty string; String (string astring) Constructs a new string String greeting = "Hello world!"; String str=new String(“Hello world!“) String (char arrofchar []): Constructs a new string and initializes it to specifies chars . char chars[] ={‘a’,’b’,’c’} String str =new String (chars) str gets initialized to abc.

5 public String(char[] value, int offset, int count)
Allocates a new String that contains characters from a subarray of the character array argument Char chars[]={‘a’,’b’,’c’,’d’,’e’,’f’}; String str=new String(chars 2,3); Initializes to :cde

6 Operations on Strings String literals :Chars enclosed in “ “.
String str =“Hello world!“ String Concatenation use + or += String str1 = “KS School”; String str2 = “of Business Management”; String str3 = “KS School”+ “of Business Management”; //Compile time expression String name = “Gujarat”; String str4 = “Gandhinagar is in ” + name; String str5 = new String(“Gandhinagar is in Gujarat”);

7 toString( ):object class has toString() that returns descriptive string.
returns a String object that contains a string. CHARACTER EXTRACTION METHODS charAt(): This method returns the character located at the String's specified index. The string indexes start from zero. Syntax: public char charAt(int index) Return Value : Returns a char at the specified index.

8 getchars() This method copies characters from this string into the destination character array. Syntax: public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) Return Value : It does not return any value but throws IndexOutOfBoundsException

9 getBytes() Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array. Syntax: public byte[] getBytes() Returns: The resultant byte array

10 toCharArray() This method converts this string to a new character array. Syntax: public char[] toCharArray() Return Value : It returns a newly allocated character array whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.

11 String Comparison Methods
equals(): compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object. Syntax: public boolean equals(Object anObject) Return Value : true if the String are equal; false otherwise.

12 equalsIgnoreCase() Syntax:
compares a String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length, and corresponding characters in the two strings are equal ignoring case . Syntax: public boolean equalsIgnoreCase(String anotherString) Return Value : true if the argument is not null and the Strings are equal, ignoring case; false otherwise.

13 regionMatches() Syntax:
This method tests if two string regions are equal. Syntax: public boolean regionMatches(int startIndex, String str2, int str2startIndex, int numchars) or public boolean regionMatches(boolean ignoreCase, int startIndex, String str, int str2startIndex, int numchars)

14 compareTo() The value 0 if strings are equal;
The value less than 0 if the argument is a string is greater than other string; The value greater than 0 if the argument is a string less than other string.

15 startsWith() and endsWith()
Determines if the given string begins with a specified string . Or ends with the specified suffix public boolean startsWith(String prefix, int toffset) Or public boolean startsWith(String prefix) public boolean endsWith(String suffix) Return Value : True/False

16 length() returns the number of characters contained in the string object. public class StringDemo{ public static void main(String args[]) { String str = “Welcome"; int len = str.length(); System.out.println( "String Length is : " + len ); } }

17 indexOf() and lastIndexOf()
public int indexOf(int ch): Returns the index within this string of the first occurrence of the specified character or -1 if the character does not occur. public int indexOf(int ch, int fromIndex): Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index or -1 if the character does not occur. int indexOf(String str): int indexOf(String str, int fromIndex): Syntax: Here is the syntax of this method: public int indexOf(int ch ) ; public int indexOf(int ch, int fromIndex) int indexOf(String str) ; int indexOf(String str, int fromIndex)

18 substring() substring(int i): This method is used to find all sub string after index i. substring(int start,int end): This is used to find the substring between start and end .

19 replace() returns a new string resulting from replacing all occurrences of oldChar in this string with newChar Syntax: public String replace(char oldChar, char newChar) Return Value : It returns a string derived from this string by replacing every occurrence of oldChar with newChar.

20 valueOf() It is a static method that is overloaded within String for all of Java's built-in types, so that each type can be converted properly into a string. valueOf( ) is also overloaded for type Object, so an object of any class type you create can also be used . static String valueOf(double num) static String valueOf(long num) static String valueOf(Object ob) static String valueOf(char chars[ ])

21 toUpperCase() and toLowerCase()
To convert all lower case characters in a string to upper case, use the method toUpperCase() of the String class. To convert all upper case characters in a string to lower case, use the method toLowerCase() of the String class.

22 StringBuffer A string buffer implements a mutable sequence of characters. A string buffer is like a String, but can be modified StringBuffer()           Constructs a string buffer with no characters in it and an initial capacity of 16 characters. StringBuffer(int length)           Constructs a string buffer with no characters in it and an initial capacity specified by the length argument. StringBuffer(String str)           Constructs a string buffer so that it represents the same sequence of characters as the string argument

23 length() . Returns the length (character count) of this string buffer.
capacity(). Returns the current capacity of the String buffer. setLength(int newLength) Sets the length of this String buffer. charAt(int index) The specified character of the sequence in string buffer, as indicated by the index argument, is returned.

24 setCharAt(int index, char ch) The character at the specified index of this string buffer is set to ch append(String str). Appends the string to this string buffer. insert(int offset, char c). Inserts the string representation of the char argument into this string buffer. reverse() The character sequence contained in this string buffer is replaced by the reverse of the sequence.

25 delete(int start, int end) Removes the characters in a substring of this StringBuffer
replace(int start, int end, String str) Replaces the characters in a substring with characters in the specified String.


Download ppt "In Java, strings are objects that belong to class java.lang.String ."

Similar presentations


Ads by Google