Presentation is loading. Please wait.

Presentation is loading. Please wait.

Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.

Similar presentations


Presentation on theme: "Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns."— Presentation transcript:

1

2 Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns : It is called on a String and 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. Possible Use : The String class represents character strings. Strings are constant, their values cannot be changed after they are created. String objects are immutable that is you can not actually change each individual character. The core benefit to using toCharArray() is that the char[ ] array you receive is mutable. For this reason, the method is ideal when you need to transform characters, as in encryption-decryption, or uppercasing the first letter.

3 Example : /* Convert String to Character Array Example This example shows how to convert a given String object to an array of character */ public class StringToCharacterArrayExample { public static void main(String args[]) { //declare the original String object String strOrig = “Hi KGEC”; //declare the char array char[] stringArray; //convert string into array using toCharArray() method of string class stringArray = strOrig.toCharArray(); //display the array for(int index=0; index < stringArray.length; index++) System.out.println(stringArray[index]); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21.

4 Output of the program would be : H i K G E C

5 Purpose : To remove the blank spaces from both ends of the invoking string (Front and End). Also to remove all ASCII control characters (such as ‘\n’, ’\t’ etc.) that may not be considered as whitespace. But it does not remove Unicode whitespace. Return Type : String Parameters : none Declaration : public String trim() Returns : It returns a copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space. Possible Use : This method is quite useful when we process user commands. For example, while taking input from user, it can be used to remove any leading or trailing whitespace and ASCII control characters that may have inadvertently been entered by the user.

6 Example : /* Removing leading and trailing blank spaces from a string */ public class TrimExample { public static void main(String args[]) { String Str = new String(“\n\t Java is object oriented \n\n”); System.out.print(“Return Value :”); System.out.print(Str.trim()); System.out.println((“ language.”); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13.

7 Output of the program would be : Return Value :Java is object oriented language.

8 Purpose : To convert all the lowercase characters in this String to uppercase. Return Type : String Parameters : none Declaration : public String toUpperCase() Returns : It copies a String and returns a reference to the new String which is converted to uppercase. The original String is unchanged. Possible Use : It is useful for processing text input or for when you need to check the string against an already uppercase string. It has no effect on non-lowercase characters.

9 Example : /* Converts all lower case characters to upper case */ public class ToUpperCaseExample { public static void main(String args[]) { //declare the original String object String strOrig = “Hello FrIeNdS”; //creating the new String object String strNew = strOrig.toUpperCase(); //display the Strings System.out.println(strOrig); System.out.println(strNew); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17.

10 Output of the program would be : Hello FrIeNdS HELLO FRIENDS

11 Purpose : To convert all the Uppercase characters in this String to lowercase. Return Type : String Parameters : none Declaration : public String toLowerCase() Returns : It copies a String and returns a reference to the new String which is converted to lowercase. The original String is unchanged. Possible Use : It is useful for processing text input or for when you need to check the string against an already lowercase string. It has no effect on non-uppercase characters.

12 Example : /* Converts all upper case characters to lower case */ public class ToLowerCaseExample { public static void main(String args[]) { //declare the original String object String strOrig = “Hello FrIeNdS”; //creating the new String object String strNew = strOrig.toLowerCase(); //display the Strings System.out.println(strOrig); System.out.println(strNew); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17.

13 Output of the program would be : Hello FrIeNdS hello friends

14 Purpose : To get a String object representing the value of Number Object. Return Type : String Parameters : int : An int for which string representation would be returned. Declaration : public String toString() Specified by: toString in interface CharSequence Overrides: toString in class Object Returns : This object (which is already a string!) is itself returned. toString(): This returns a String object representing the value of this Integer. toString(int i): This returns a String object representing the specified integer. Possible Use : toString() allows the resulting strings to be fully integrated into Java's programming environment. They can be used in print() and println() statements and in concatenation expressions. More often it is used for debugging purposes.

15 Example : /* Converts Integer object to String */ public class ToStringExample { public static void main(String args[]) { Integer x = 5; System.out.println(x.toString()); System.out.println(Integer.toString(12)); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

16 Output of the program would be : 5 12

17 Purpose : To convert data from its internal format into a human-readable String form. Return Type : String Parameters : primitive data type, String, Object etc. Declaration : public static String valueOf( ) Returns : This method has followings variants which depends on the passed parameters. This method returns the string representation of the passed argument. valueOf(boolean b): Returns the string representation of the boolean argument. valueOf(char c) : Returns the string representation of the char argument. valueOf(char[] data) : Returns the string representation of the char array argument. valueOf(char[] data, int offset, int count) : Returns the string representation of a specific subarray of the char array argument. valueOf(double d) : Returns the string representation of the double argument. valueOf(float f) : Returns the string representation of the float argument. valueOf(int i) : Returns the string representation of the int argument. valueOf(long l) : Returns the string representation of the long argument. valueOf(Object obj) : Returns the string representation of the Object argument.

18 Example : /* Converts primitive datatype to String */ public class ValueOfExample { public static void main(String args[]) { double d = 102939939.939; boolean b = true; long l = 1232874; char[] arr = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}; System.out.println("For double : " + String.valueOf(d)); System.out.println("For boolean : " + String.valueOf(b)); System.out.println("For long : " + String.valueOf(l)); System.out.println("For chararray : " + String.valueOf(arr)); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. Possible Use : It is useful during concatenation operations when a string representation of some other type of data is needed.

19 Output of the program would be : For double : 1.02939939939E8 For boolean : true For long : 1232874 For chararray : abcdefg

20

21 What is StringBuffer? The StringBuffer class is used to represent a sequence of characters that can be modified. That’s why StringBuffer class is a mutable class. StringBuffer has a char[] array in which it keeps the strings that we append to it. Every StringBuffer has a capacity. The amount of memory currently allocated to that buffer is the capacity. It increases automatically as more contents added to it. The amount currently used is the length. StringBuffers are preferred when heavy modification of character strings is involved (such as appending, inserting, deleting, modifying etc). Strings can be obtained from StringBuffers. Since the StringBuffer class does not override the equals() method from the Object class, contents of string buffers should be converted to String objects for string comparison. So what is the difference? String is used to manipulate character strings that cannot be changed (read- only and immutable). Whereas StringBuffer is used to represent characters that can be modified (mutable).

22 Purpose : To know the current capacity of the String buffer. The capacity is the amount of storage available for newly inserted characters; beyond which an allocation will occur. Return Type : int Parameters : none Declaration : public int capacity() Returns : The current capacity of this string buffer as an integer value. Possible Use : The capacity and length methods can be used to work with the number of characters in the object. Also to know how many of the array positions are actually valid.

23 Example - 1 : /* To see the current capacity of the StringBuffer */ public class StringBufferCapacityExample { public static void main(String args[]) { StringBuffer sb = new StringBuffer("Hello Friends"); System.out.println("Content of String Buffer : " + sb); int len = sb.length(); int cap = sb.capacity(); System.out.println("Length of String Buffer : " + len); System.out.println("Capacity of String Buffer : " + cap); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17.

24 Output of the program would be : Content of String Buffer : Hello Friends Length of String Buffer : 13 Capacity of String Buffer : 29

25 Example - 2 : /* To illustrate StringBuffer Constructors */ public class StringBufferExample { public static void main(String args[]) { StringBuffer strBuf1 = new StringBuffer("KGEC"); StringBuffer strBuf2 = new StringBuffer(100); //With capacity 100 StringBuffer strBuf3 = new StringBuffer(); //Default Capacity 16 System.out.println("strBuf1 : " + strBuf1); System.out.println("strBuf1 capacity : " + strBuf1.capacity()); System.out.println("strBuf2 capacity : " + strBuf2.capacity()); System.out.println("strBuf3 capacity : " + strBuf3.capacity()); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.

26 Output of the program would be : strBuf1 : KGEC strBuf1 capacity : 20 strBuf2 capacity : 100 strBuf3 capacity : 16

27 Purpose : To concatenates the string representation of any other type of data to the end of the invoking StringBuffer object. Return Type : StringBuffer Parameters : primitive data type, String, Object etc. Declaration : public StringBuffer append( ) public StringBuffer append(boolean b) public StringBuffer append(char c) public StringBuffer append(char[] str) public StringBuffer append(char[] str, int offset, int len) public StringBuffer append(double d) public StringBuffer append(float f) public StringBuffer append(int i) public StringBuffer append(long l) public StringBuffer append(Object obj) public StringBuffer append(StringBuffer sb) public StringBuffer append(String str)

28 Example : /* To illustrate append() method */ public class StringBufferAppendExample { public static void main(String args[]) { StringBuffer sb = new StringBuffer(“Test"); sb.append(" String Buffer"); //Append and Overwrite sb System.out.println(sb); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. Returns : A reference to this StringBuffer object. Possible Use : The append( ) method is most often called when the + operator is used on String objects. It is also used by the compiler to implement the binary string concatenation operator +.

29 Output of the program would be : Test String Buffer

30


Download ppt "Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns."

Similar presentations


Ads by Google