Type to search

programming java

Strings in java: All the Methods and Definition

Share

In Today’s world data is everywhere. Without data, nothing seems to happen from everyday life to business everything in the form on the top of data. One type of data that is from held the beginning is known to be text. In Java, we can store text with the help of the String Class. The
string class is an array of characters.
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared

Let us begin with the basics

Syntax

String str = new String(“Hello”);
String strNew = “World”;
String str1 = “”; // empty string
String str2 = null;

Note: Know the difference between the empty string and the null value.

An empty string is initialized with the array without any values but the null string is not even initialized

For example

System.out.println(str1.length()); // gives 0 as its empty
System.out.println(str2.length()); // throws NullPointerException since the Object is not initialized

PS : int length() returns the length of the string

Concating strings are most used operators on the string

Example

str = str.concat(strNew); // HelloWorld String -- class provides concat
api for this use case
str1 = str + " " + strNew; // HelloWorld World -- you can also achieve
with this using + operator too

Some important methods of Strings are :

int indexOf(int ch)

Returns the index within this string of the first occurrence of the specified character. It returns -1 if the specified character is not found in the String

str = "Hello world!";
System.out.println(str.indexOf('l')); // 

Overloaded methods

  • indexOf(int ch, int fromIndex)
  • indexOf(String str)
  • indexOf(String str, int fromIndex)

Similar methods

lastIndexOf(int ch)
lastIndexOf(int ch, int fromIndex)
lastIndexOf(String str)
lastIndexOf(String str, int fromIndex)
Note: if the fromIndex is specified then it searches from that specific index

char charAt(int index)

Returns the char value at the specified index.

str = "Hello world!"; 
System.out.println(str.charAt(4)); // o 

Note: if the specified index not in the range 0 <= index < str.length() then it throws StringIndexOutOfBoundsException 

boolean contains(CharSequence s)

Returns true if and only if this string contains the specified sequence of char values.

str = "Hello world!"; 
System.out.println(str.contains("ello")); // true

int compareTo(String anotherString)

Compares two strings lexicographically. Return the first character difference other wise 0

System.out.println("Hello".compareTo("Hello")); //0
System.out.println("hello".compareTo("Hello")); //32 
System.out.println("Hello".compareTo("hello")); //-32 

Similar methods 

compareToIgnoreCase(String str) // it does the same but ignores the case difference 

boolean startsWith(String prefix)

Tests if this string starts with the specified prefix.

str = "Hello world!"; 
System.out.println(str.startsWith("ello")); // false
System.out.println(str.startsWith("Hel")); // true 

Overloaded methods 

startsWith(String prefix, int offset) //checks from the offset 

boolean endsWith(String suffix)

Tests if this string ends with the specified suffix.

 str = "Hello world!";
 System.out.println(str.endsWith("ello")); // false
 System.out.println(str.endsWith("d!")); // true 

String substring(int beginIndex)

Returns a new string that is a substring of this string.

str = "Hello World!"; 
System.out.println(str.substring(2)); //llo World! 
System.out.println(str.substring(2, 5)); //llo 

Overloaded methods 

substring(int beginIndex, int endIndex) 

char[] toCharArray()

Converts this string to a new character array. 

String str = "Hello World!";
char[] characters = str.toCharArray();
System.out.println(characters); //Hello World! 

String toLowerCase()

Converts all of the characters in this String to lower case using the rules of the default locale.

String str = "Hello World!"; 
System.out.println(str.toLowerCase()); //hello world! 

Note: We can also change the default Locale using this API. 

String toUpperCase()

Converts all of the characters in this String to upper case using the rules of the default locale. 

String str = "Hello World!";
System.out.println(str.toUpperCase()); //HELLO WORLD! 

Note: We can also change the default Local using this API. 

String trim()

It deletes the leading and trailing spaces in the string. 

String str = " _ Hello World! _ ";
System.out.println(str.trim()); //_ Hello World! _ 

String replace(char oldChar, char newChar)

Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar. 

String str = "Hello World!";
System.out.println(str.replace('l','i')); //Heiio Worid! 

Similar methods 

replace(CharSequence target, CharSequence replacement)

replaceAll(String regex, String replacement)

replaceFirst(String regex, String replacement) 

String[] split(String regex)

Splits this string around matches of the given regular expression. It returns an array of Strings. 

String str = "Hello World! This is the start of the every experiment"; 
String[] words = str.split("\\s");
System.out.println(Arrays.toString(words)); // [Hello, World!, This, is, the, start, of, the, every, experiment] 

Overloaded methods 

split(String regex, int limit) 

Converting Other data types to String 

Some times we need to convert any data type to String. For those use cases, we have the following methods. Check that out 

Now you got a basic idea about the strings. Do check this for more methods

Leave a Comment

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