Process Advisors

ey-logo
*Subject to Terms and Condition

What is String in java

String is a collection of characters but in this language string is an object which represents a collection of characters. To create string object String class is used. An array of characters is known as String.

Create Strings

String name = “intellipaat”; OR
char[] name={'i','n','t','e','l','l','i','p','a','a',’t’};

The java.lang.String class implements Serializable, Comparable and CharSequence interfaces.
The java String is immutable i.e. it cannot be changed but a new instance is created. For mutable class, you can use StringBuffer and StringBuilder class.
There are two ways to create String object:

  1. By string literal
  2. By new keyword

 

1. By String Literal

It is created by using double quotes. The area in which string objects are stored is known as string constant pool.
 e.g.

String name="intellipaat";

Learn Java

2. By new keyword

There is another way to create string object using new keyword like as follows:

String name=newString("intellipaat");

Java String class methods

The java.lang.String class contains number of methods which are used to perform various operations:

Method Description
int length() Returns string length
char charAt(int index) Returns char value for the specified index
static String format(String format, Object… args) Returns formatted string
String substring(int StartingIndex) Returns substring for given start index
String substring(int StartingIndex, int EndingIndex) Returns substring for given start index and end index
boolean contains(CharSequence s) Returns true or false after matching the sequence of char value
boolean equals(Object another) Check the equality of string with object
String toLowerCase() Returns string in lowercase.
String toUpperCase() Returns string in uppercase.
boolean isEmpty() Check string is empty or not. If yes than return true otherwise false
String trim() Returns trimmed string omitting leading and trailing spaces
String replace(char old_character , char new_character) Replaces all occurrences of specified char value
String replace(CharSequence old, CharSequence new) Replaces all occurrences of specified CharSequence
String split(String regex) Returns splitted string matching regex
int indexOf(int ch) Returns specified char value index
int indexOf(int ch, int fromIndex) Returns specified char value index starting with given index
int indexOf(String substring) Returns specified substring index
int indexOf(String substring, int fromIndex) Returns specified substring index starting with given index

Course Schedule

Name Date Details
Python Course 10 Jun 2023(Sat-Sun) Weekend Batch
View Details
Python Course 17 Jun 2023(Sat-Sun) Weekend Batch
View Details
Python Course 24 Jun 2023(Sat-Sun) Weekend Batch
View Details

Leave a Reply

Your email address will not be published. Required fields are marked *