String Notes
String Notes
String Notes
A sequence of character data enclosed in double quotes is called a string. Strings of Java
work differently from that of C and C + +. In C, string is an array of characters with a
terminating \0. But in Java, string is an object of String class.
That is manipulation of strings is quite different from C and in Java, it is very easy due the
rich methods of String class. For example, we can concatenate strings with + operator.
Java platform provides two string classes to manipulate strings String, for constant strings
and StringBuffer, for strings that can change.
Strings are immutable. That is, strings once created cannot be changed at the same memory
location. Whenever a new value is assigned to a string, a new memory location is created and
the new value is stored in it and the old location (with old value) is garbage collected.
It is definitely a overhead to the operating system. But this is made to increase the
performance and for the same reason String class is declared as final.
Methods
The java.lang.String class provides a lot of methods to work on string. By the help of these
methods, we can perform operations on string such as trimming, concatenating, converting,
comparing, replacing strings etc.
Java String is a powerful concept because everything is treated as a string if you submit
any form in window based, web based or mobile application.
The java string toUpperCase() method converts this string into uppercase letter and string
toLowerCase() method into lowercase letter.
String s="Sachin";
System.out.println(s.toUpperCase());//SACHIN
System.out.println(s.toLowerCase());//sachin
System.out.println(s);//Sachin(no change in original)
2. Java String trim() method
The string trim() method eliminates white spaces before and after string
String s=" Sachin ";
System.out.println(s);// Sachin
System.out.println(s.trim());//Sachin
3. charAt() method
The string charAt() method returns a character at specified index
String s="Sachin";
System.out.println(s.charAt(0));//S
System.out.println(s.charAt(3));//h
4. length() method
The string length() method returns length of the string.
String s="Sachin";
System.out.println(s.length());//6
5. valueOf () method
int a=10;
String s=String.valueOf(a);
System.out.println(s+10);
6. replace() method
String s1="Java is a programming language. Java is a platform. Java is an Island.";
String replaceString=s1.replace("Java","Kava");//replaces all occurrences of "Java" to "K
ava"
System.out.println(replaceString);
StringBuffer class
Java StringBuffer class is used to create mutable (modifiable) string. The StringBuffer class
in java is same as String class except it is mutable i.e. it can be changed.
Constructor
StringBuffer()
StringBuffer(String str)
StringBuffer(int variable)
Methods
There are various methods but only few have been mentioned below.
1. append(String s) - is used to append the specified string with this string.
2. insert(int offset, String s)- is used to insert the specified string with this string at
the specified position
3. replace(int startIndex, int endIndex, String str)- is used to replace the string
from specified startIndex and endIndex
4. delete(int startIndex, int endIndex)- is used to delete the string from specified
startIndex and endIndex.
5. reverse()- is used to reverse the string.
6. charAt(int index)- is used to return the character at the specified position.
7. length()- is used to return the length of the string
Write a Java program to get string and count number of words in provided string
import java.util.Scanner;