String
Original Scanned PDF – View Notes
Introduction To String
- A string is an object that represents a sequence of characters.
Java.lang.Stringclass is used to create a string object.- Double quotes (
" ") is used to represent string constant. - For example:
"Ram"is a string containing a sequence of characters'R','a'and'm'.
String Declaration
String can be declared by two ways.
- By String literal
- By new keyword
1) By String Literal
It is direct method to create string using
Syntax
String stringname = "string";Example
String s1 = "Ram";
String s2 = "welcome";Java Program to Create String by String Literal
public class StringTest
{
public static void main(String []args)
{
String s1 = "Ram"; // creating string
String s2 = "Hello Java";
System.out.println("First string is =" + s1);
System.out.println("Second string is =" + s2);
}
}2) By new Keyword
- The new keyword is used to create objects including String.
- It is less common and typically done when we want to create string explicitly.
Syntax
String str = new String("String");Example
String str = new String("Hello Java");Here, a new string object str is created with the value "HelloJava". The new keyword is followed by the string constructor which takes a string literal "HelloJava" as its argument.
Program to Create String Using new Keyword
public class StringTest
{
public static void main(String []args)
{
String s1 = "Java"; // creating string by string literal
String s2 = new String("Programming");
// creating string using new keyword
System.out.println("First string :" + s1);
System.out.println("Second String :" + s2);
}
}String Manipulation Methods / Functions
Java.lang.String class includes many methods for manipulating on string.
Some methods are:
1) length() Method
- Used to find length of given string.
- It returns no. of characters included within string.
Syntax
String.variable.length();Example to Demonstrate length() Method
import java.lang.String;
class Stringlength
{
public static void main(String []args)
{
String str = "welcome"; // creating string
System.out.println("String is =" + str);
int len = str.length(); // method calling
System.out.println("The length of String =" + len);
}
}2) concat() Method
This method join or concat two string and form a single string.
Syntax
str.Var1.concat(str.Var2);concat(). The source wording is preserved below.
Example to Demonstrate length() Method
import java.lang.String;
public class Stringlength
{
public static void main(String []args)
{
String s1 = "Ram"; // creating string
String s2 = "Hari";
System.out.println("First string :" + s1);
System.out.println("Second string :" + s2);
String s3 = s1.concat(s2);
System.out.println("concatenated string :" + s3);
}
}3) equals()
- This method is used to make comparison between two strings to check whether strings are equal or not.
- It returns logical value either true or false.
Syntax
str1.equals(str2);Example
result, and the second comparison is visibly written as s1.equals(3). These details are retained rather than silently corrected.
import java.lang.String;
public class
{
public static void main(String []args)
{
String s1 = "Java";
String s2 = "Java";
String s3 = "Programming";
boolean result = s1.equals(s2);
System.out.println("First and second strings are equal :" + result);
boolean result = s1.equals(3);
System.out.println("First and third strings are equal :" + result);
}
}StringBuffer Class
- StringBuffer is a class included within
java.iopackage. - It is used to create mutable (changable) string objects.
- It is same as String class except it is mutable i.e. can be modified the content of object after it has been created.
- It provides an alternative to the immutable String class.
Some Methods of StringBuffer Class Are:
- append()
- insert()
- replace()
- delete()
- reverse()
1) append() Method
Used to concatenate (join) the given argument with string.
Syntax
obj.append("String");Example
import java.io.*;
public class StringTest
{
public static void main(String []args)
{
StringBuffer sb = new StringBuffer("Hello");
sb.append("Java");
System.out.println("New string =" + sb);
}
}2) insert()
This method inserts the given string with this string at the given position.
Syntax
sb.insert(position, "new string");Example
import java.io.*;
class program
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello");
sb.insert(1, "java"); // original string is changed
System.out.println("New string :" + sb);
}
}HJavaello
3) Replace()
This method replaces the given string from the specified begin index and endIndex − 1.
Syntax
replace(beginIndex, endIndex - 1, "string");Example
import java.io.*;
class program
{
public static void main(String []args)
{
StringBuffer sb = new StringBuffer("Hello");
sb.replace(1, 3, "Java");
System.out.println(sb);
}
}HJavaO
4) delete() Method
This method delete the string from specified begin Index to endIndex − 1.
Syntax
delete(beginIndex, endIndex - 1)Example
import java.io.*;
class Program
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello");
sb.delete(1, 3);
System.out.println(sb);
}
}5) reverse() Method
This method reverse the current string.
Syntax
sb.reverse();
where sb is StringBuffer string objectExample
import java.io.*;
class Reverse
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello");
sb.reverse();
System.out.println(sb);
}
}StringBuilder Class
- It is mutable class located in
java.langpackage used to manipulate string. - It is alternative to String class.
- Similar to StringBuffer class but differs on the basic of synchronization.
Consist of Following Methods:
- append() method
- insert() method
- delete() method
- replace() method
- reverse() method
1) append() Method
Used to concatenate (Join) the given argument with string.
Syntax
obj.append("String");Example
import java.lang in the scanned notes. It is preserved as shown.
import java.lang
public class Append
{
public static void main(String args[])
{
StringBuilder sb = new StringBuilder("Hello");
sb.append("Java");
System.out.println(sb);
}
}2) insert() Method
This method insert the given string with this string at the given position.
Syntax
sb.insert(position, "new string");Example
import java.lang
class Insert
{
public static void main(String args[])
{
StringBuilder sb = new StringBuilder("Hello");
sb.insert(1, "Java");
System.out.println("New string :" + sb);
}
}3) delete() Method
This method deletes the string from specified beginIndex to endIndex.
Syntax
delete(beginIndex, endIndex - 1)Example
import java.lang
class Delete
{
public static void main(String args[])
{
StringBuilder sb = new StringBuilder("Hello");
sb.delete(1, 3);
System.out.println(sb);
}
}4) Replace() Method
This method replaces the given string from the specified beginIndex and endIndex.
Syntax
replace(beginIndex, endIndex, "String");Example
import java.lang
class Replace
{
public static void main(String args[])
{
StringBuilder sb = new StringBuilder("Hello");
sb.replace(1, 3, "Java");
System.out.println(sb);
}
}5) reverse() Method
This method reverse the current string.
Syntax
sb.reverse();Example
import java.lang
class Reverse
{
public static void main(String []args)
{
StringBuilder sb = new StringBuilder("Hello");
sb.reverse();
System.out.println(sb);
}
}
Discussion
Share a helpful question, idea, or explanation with other students.