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

Slides:



Advertisements
Similar presentations
I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
Advertisements

Basic Java Constructs and Data Types – Nuts and Bolts
Problem Solving & Program Design in C
9 Copyright © 2005, Oracle. All rights reserved. Using Strings, String Buffer, Wrapper, and Text-Formatting Classes.
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
The ArrayList Class and the enum Keyword
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
CSci 1130 Intro to Programming in Java
Introduction to Programming G51PRG University of Nottingham Revision 1
String Pemrograman Berbasis Obyek Oleh Tita Karlita.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
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.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
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.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
©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,
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.
28-Jun-15 String and StringBuilder Part I: String.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
 Pearson Education, Inc. All rights reserved Strings, Characters and Regular Expressions.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
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.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Working with string In java Four classes are provided to work with String:1) String 2)String Buffer 3)String Tokenizer 4)String Builder Note: An object.
Chapter 7: Characters, Strings, and the StringBuilder.
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 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,
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
1 Java Strings Dr. Randy M. Kaplan. 2 Strings – 1 Characters are a fundamental data type in Java It is common to assemble characters into units called.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
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,
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.
1 Unit-2 Arrays, Strings and Collections. 2 Arrays - Introduction An array is a group of contiguous or related data items that share a common name. Used.
String and StringBuffer classes
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Strings.
EKT 472: Object Oriented Programming
Multiple variables can be created in one declaration
Modern Programming Tools And Techniques-I Lecture 11: String Handling
String and StringBuilder
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
String and StringBuilder
String and StringBuilder
Java – String Handling.
String and StringBuilder
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:

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.

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]); }

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

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.

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.”); }

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

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.

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); }

Output of the program would be : Hello FrIeNdS HELLO FRIENDS

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.

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); }

Output of the program would be : Hello FrIeNdS hello friends

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.

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)); }

Output of the program would be : 5 12

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.

Example : /* Converts primitive datatype to String */ public class ValueOfExample { public static void main(String args[]) { double d = ; boolean b = true; long l = ; 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)); } Possible Use : It is useful during concatenation operations when a string representation of some other type of data is needed.

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

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).

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.

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); }

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

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()); }

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

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)

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); } 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 +.

Output of the program would be : Test String Buffer