Home » Java programming language

Operations on string in Java (functions/methods of String class)

Java String Operations, Functions/Methods with Example: Here, we will learn about the Strings in Java, String Operations and Function/Methods of String class with Examples.
Submitted by IncludeHelp, on July 03, 2018

String is a class in java, which provides some of the predefined methods that make string based problem solutions easier. We don’t need to write code for every operation, we have to just use its methods.

In this post, we are going to learn some of the most useful methods of the string.

Declaring and assigning value to string objet

1 ) Declaration and assignment (separate statements)

    String msg;	//declaration
    msg = "Hello world"; // assignment

2 ) Declaration with initialization

    String msg = "Hello world";

Java String class methods

1) s1.equals(s2)

This function is used to compare two strings; it returns boolean values ‘true’/ ‘false’. If s1 and s2 are exactly same it returns ‘true’ otherwise it returns ‘false’.


    s1 = "Hello world"
    s2 = "Hello world"

Both strings are exactly same, so the function will returns ‘true’

2) s1.equalsIgnoreCase(s2)

This function is also used to compare two strings but it ignores the case, it returns boolean values ‘true’/ ‘false’. If s1 and s2 are same (by ignoring the case) it returns ‘true’ otherwise it returns ‘false’.


    s1 = "Hello world"
    s2 = "HELLO world"

In this case, strings are same but characters are not in same case, still this function will return ‘true’.

3) s1.length()

This function returns the length of the string s1 i.e. total number of characters of the string.


    String s1 = "Hello world!";

Total number of characters in the string "Hello world!" are 12. Therefore, this function will return 12.

4) s1.charAt(N)

This function is used to get the character from Nth index of the string. Remember, string’s index starts from 0.


    String msg = "Hello world";

Output of this example with be "H" and "w" because "H" is at the 0th index and "w" is at the 6th index.

Read more: Java String | String.charAt(index) Method with Example

5) s1.indexOf(s2)

This function is used to get the starting index of any substring. Here, if substring s2 exists in the string s1, it will return starting position (index) of substring s2. If substring does not exist in the string, it returns -1.

Example: (Case 1: If substring exists in the string)

    String msg = "Hello world";

Output will be 6, because substring "world" initial index starts from 6th in the string "Hello world".

Example: (Case 2: If substring does not exist in the string)

    String msg = "Hello world";

Output will be -1, because substring "Hi" does not exist in the string "Hello world".

6) s1.substring(N,M)

This function is used to get the substring from the string. Here, function substring() will return the substring starting from Nth index to (M-1)th index.

Let suppose value of N is 6 and value of M is 11, then function will return 6th, 7th, 8th, 9thth and 10th character, it does not consist Mth character.


    String msg = "Hello world!";

Output will be "world".

7) s1.compareTo(s2)

This function is used to compare two strings, if both strings are same it returns 0, if string s1 is less than s2 it returns negative value, if string s2 is less than string s1 it returns positive value. Negative and positive value depends on the difference in the ASCII codes of first dissimilar characters.


    String s1,s2;

    s1 = "Hello";
    s2 = "Hello";

    s1 = "Hello";
    s2 = "HELLO";

    s1 = "Hello";
    s2 = "World";



8) s1.toUpperCase()

This function returns uppercase string.


    String msg = "Hello World!";

Output will be "HELLO WORLD!".

9) s1.toLowerCase()

This function returns lowercase string.


    String msg = "Hello World!";

Output will be "Hello world!".

10) s1.trim()

This function returns the trimmed string after removing leading and trailing spaces.


    String msg = " Hello world! ";

Output will be "Hello world!".

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.