Strings In Java

Java Strings are discussed, to better meet the demands of the learners.

Java Strings

A string is used to store text. It consists of a collection of characters surrounded by double quotes.

Assign a value to a String variable:

Example: 

public class Main{ public static void main(String[] args) { String mrx_name="We are learning about Java Strings ";System.out.println("Text = "+mrx_name);} // Output = Text = We are learning about Java Strings };

Another Example: 

public class Main{ public static void main(String[] args) { String ample_text="Hey Everyone !!"; String mrx_name=" We are learning about Java Strings ";System.out.println(ample_text+mrx_name);}// Output = Hey Everyone !! We are learning about Java Strings };


String Length

Java strings are actually objects that contain methods to perform certain operations on strings.

A string’s length can be found with the length() method, for instance:

Example: 

public class Main{ public static void main(String[] args) { String mrx_name="We are counting the length of the string using length() method "; System.out.println("length of String= "+mrx_name.length());}// Output = length of String= 60 };

Example: 

public class Main{ public static void main(String[] args) { String mrx_name="12345678910"; System.out.println("length of String= "+mrx_name.length());}// Output = length of String= 11 };

More String Methods

There are a variety of methods in String, for example toUpperCase() and toLowerCase():

Example: 

public class Main{ public static void main(String[] args) { String mrx_name="We are learning toUpperCase and toLowerCase methods of String "; System.out.println("Uppercase: "+mrx_name.toUpperCase()); System.out.println("Lowercase: "+mrx_name.toLowerCase());}// Output 1 = Uppercase: WE ARE LEARNING TOUPPERCASE AND TOLOWERCASE METHODS OF STRING // Output 2= Lowercase: we are learning touppercase and tolowercase methods of string };

Another Example: 

public class Main{ public static void main(String[] args) { String mrx_name="Java is an object oriented programming language"; System.out.println("Uppercase: "+mrx_name.toUpperCase()); System.out.println("Lowercase: "+mrx_name.toLowerCase());}// Output 1 = Uppercase: JAVA IS AN OBJECT ORIENTED PROGRAMMING LANGUAGE // Output 2= Lowercase: java is an object oriented programming language };

The indexOf() method returns the position of the first instance of a specified text in a string (including whitespace) along with the index (the number) at which it appears:

Example: 

public class Main{ public static void main(String[] args) { String txt = "Return index of word Java !"; System.out.println("Index number= "+txt.indexOf("Java")); //}// Output: Index number= 21 };

 

Example: 

public class Main{ public static void main(String[] args) { String txt = "1 2 3 4 5 6 7 8 9 "; System.out.println("Index number of 6 = "+txt.indexOf("6")); //}// Output: Index number of 6 = 10 };
Java starts counting locations from zero.

A string’s initial position is 0, its second is 1, and its third place is 2.


Java Strings Methods

MethodsOverview
contains()Checks if a substring exists in the string.
substring()Substrings of strings are returned.
join()Strings are joined using the delimiter.
replace()Specifies a new character to replace the old one.
replaceAll()Substrings matching a regex pattern are replaced.
replaceFirst()First matching substring should be replaced.
charAt()The character present in the specified location will be returned.
getBytes()Bytes are converted from the string.
indexOf()In a string, this function returns the position of the specified character.
compareTo()The dictionary order is used to compare two strings.
compareToIgnoreCase()Case differences are ignored when comparing two strings.
trim()Removes all leading and trailing whitespaces.
format()A formatted string is returned.
split()Divides a string into an array.
toLowerCase()Lowercases the string.
toUpperCase()Uppercases the string
valueOf()The specified argument will be returned as a string.
toCharArray()This function converts the string into a char array.
matches()Matches the string against the regex..
startsWith()This method checks if a string begins with a given string.
endsWith()A string is checked to see if it ends with the given string.
isEmpty()An empty string is checked or not.
intern()This function returns the string’s canonical representation.
contentEquals()A string is compared to charSequence to determine if it is equal
hashCode()The string’s hash code is returned.
subSequence()The string is subsequenced by this method.

Visit our Java String Methods Reference for a comprehensive list of String methods.

All string methods are covered in the reference, along with descriptions and examples.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

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