0% found this document useful (0 votes)
6 views

String Function

The document provides an overview of various Java String methods, including charAt(), length(), concat(), toLowerCase(), toUpperCase(), trim(), indexOf(), and replace(). Each method is explained with its syntax, functionality, and example code demonstrating its usage. The document highlights how these methods manipulate strings in Java, including character retrieval, string length calculation, concatenation, case conversion, whitespace trimming, substring indexing, and character replacement.

Uploaded by

ruahabhjadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

String Function

The document provides an overview of various Java String methods, including charAt(), length(), concat(), toLowerCase(), toUpperCase(), trim(), indexOf(), and replace(). Each method is explained with its syntax, functionality, and example code demonstrating its usage. The document highlights how these methods manipulate strings in Java, including character retrieval, string length calculation, concatenation, case conversion, whitespace trimming, substring indexing, and character replacement.

Uploaded by

ruahabhjadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Java String charAt()


The Java String class charAt() method returns a char value at the given
index number.

The index number starts from 0 and goes to n-1, where n is the length of the
string. It returns StringIndexOutOfBoundsException, if the given index
number is greater than or equal to this string length or a negative number.

Syntax

1. public char charAt(int index)

Example:

public class CharAtExample{


public static void main(String args[]){
String name="javatpoint";
char ch=name.charAt(4);//returns the char value at the 4th index
System.out.println(ch);
}}

Output: t

2 Java String length()


The Java String class length() method finds the length of a string. The
length of the Java string is the same as the Unicode code units of the string.

Signature
The signature of the string length() method is given below:

1. public int length()


Example:

public class LengthExample{


public static void main(String args[]){
String s1="javatpoint";
String s2="python";
System.out.println("string length is: "+s1.length());//10 is the length of
javatpoint string
System.out.println("string length is: "+s2.length());//6 is the length of p
ython string
}}
2. string length is: 10
OTUPUT:
string length is: 10
string length is: 6

3.Java String concat


The Java String class concat() method combines specified string at the
end of this string. It returns a combined string. It is like appending another
string.

Signature
The signature of the string concat() method is given below:

1. public String concat(String anotherString)

Example:

public class ConcatExample{


public static void main(String args[]){
String s1="java string";
/ The string s1 does not get changed, even though it is invoking the meth
od
// concat(), as it is immutable. Therefore, the explicit assignment is req
uired here.
s1.concat("is immutable");
System.out.println(s1);
s1=s1.concat(" is immutable so assign it explicitly");
System.out.println(s1);
}}

Output:

java string

java string is immutable so assign it explicitly

4.Java String toLowerCase()


The java string toLowerCase() method returns the string in lowercase
letter. In other words, it converts all characters of the string into lower case
letter.

public class StringLowerExample{


public static void main(String args[]){
String s1="JAVATPOINT HELLO stRIng";
String s1lower=s1.toLowerCase();
System.out.println(s1lower);
}}

Output:

javatpoint hello string

5 Java String toUpperCase()


The java string toUpperCase() method returns the string in uppercase
letter. In other words, it converts all characters of the string into upper case
letter.

public class StringUpperExample{


public static void main(String args[]){
String s1="hello string";
String s1upper=s1.toUpperCase();
System.out.println(s1upper);
}}

Output:

HELLO STRING

6 Java String trim()


The Java String class trim() method eliminates leading and trailing spaces.
The Unicode value of space character is '\u0020'. The trim() method in Java
string checks this Unicode value before and after the string, if it exists then
the method removes the spaces and returns the omitted string.

public class StringTrimExample{


public static void main(String args[]){
String s1=" hello string ";
System.out.println(s1+"javatpoint");//without trim()
System.out.println(s1.trim()+"javatpoint");//with trim()
}}

Output

hello string javatpoint

hello stringjavatpoint
8 Java String indexOf()
The Java String class indexOf() method returns the position of the first
occurrence of the specified character or string in a specified string.

public class IndexOfExample2 {


public static void main(String[] args) {
String s1 = "This is indexOf method";
// Passing Substring
int index = s1.indexOf("method"); //Returns the index of this substring
System.out.println("index of substring "+index);
}

Output:

index of substring 16

9 Java String replace()


The Java String class replace() method returns a string replacing all the
old char or CharSequence to new char or CharSequence.

public String replace(char oldChar, char newChar)

public class ReplaceExample1{


public static void main(String args[]){
String s1="javatpoint is a very good website";
String replaceString=s1.replace('a','e');//replaces all occurrences of 'a' to 'e'
System.out.println(replaceString);
}}
Output: jevetpoint is e very good website

You might also like