Substring Based on a Character in Java

String class provides us with powerful String processing methods, out of those, substring is one. In this example, we’ll look into examples on substring based on a given character in Java.

Unfortunately, String class doesn’t provide us with an out of box substring method to get a substring based on a character, however you can use indexOf method in combination of substring to achieve what we want.

Get Substring Which is After a Character in Java

In this example, we will read a substring of a String from a given character till the end using the substring and indexOf functions.

package com.techstackjournal;

public class SubstringMain {

	public static void main(String[] args) {

		String str = "Hello World Java";

		System.out.println(str.substring(str.indexOf('W')));

	}

}
  • The indexOf function will return the index of the given character
  • Once we receive the index from indexOf method, the substring method will use that index as the begin index
  • The returned value will be “World Java”

Output:

World Java

Get Substring Which is Before a Character in Java

In this example, we want to return a substring from start till a given character.

package com.techstackjournal;

public class SubstringMain {

	public static void main(String[] args) {

		String str = "Hello World Java";

		System.out.println(str.substring(0, str.indexOf('d') + 1));

	}

}
  • We want to read “Hello World” from “Hello World Java”
  • We’ve used substring method and passed 0 as the first argument, as we want to read the String from start
  • In the second argument, we are calling indexOf method to return the index of the desired character
  • We’ve passed character d which is the last letter of desired string as an argument to indexOf method to get the index
  • However, you need to note that substring method will return the string starting from begin index till the preceding letter of the end index (endIndex -1), that is the reason why we add 1 to the end index
  • The substring method, using these begin and end indexes return “Hello World”

Output:

Hello World

Get Substring from One Character to Another Character

In this example, we want the substring from one character to another character, that is, from ‘W’ to ‘d’.

package com.techstackjournal;

public class SubstringMain {

	public static void main(String[] args) {

		String str = "Hello World Java";
		System.out.println(str.substring(str.indexOf('W'), str.indexOf('d') + 1));

	}

}
  • We want to read from character ‘W’ to ‘d’, that is, “World” from “Hello World Java”
  • Since, we want the string to be returned from 2 indexes we have used substring method with begin index and end index
  • For begin index, we have passed character ‘W’ to indexOf method
  • For end index, we have passed character ‘d’ to indexOf method
  • We’ve added +1 to end index as substring will return String till preceding character of end index
  • This entire substring method call will return “World” as desired

Output:

World

Get Substring Between Two Characters

In this example, we want to read substring between two characters, for example, substring between ‘W’ and ‘d’ is “orl”.

package com.techstackjournal;

public class SubstringMain {

	public static void main(String[] args) {

		String str = "Hello World Java";

		System.out.println(str.substring(str.indexOf('W') + 1, str.indexOf('d')));

	}

}
  • We want to read substring between two characters ‘W’ and ‘d’, that is, “orl”
  • We are getting the required indexes of ‘W’ and ‘d’ using indexOf method
  • Since we need the characters between two characters, we need to fetch next letter of ‘W’, for this reason we added +1 to the index of ‘W’
  • As substring anyhow returns the string till the preceding index we don’t have to subtract from the index of ‘d’
  • The resulting string would be “orl”

Output:

orl

Get Substring Based on a Character by Ignoring the Case

In this example, we’ll get a the substring based on a character but by ignoring the case of given character. For example, we want “World Java” to be returned from “Hello World Java” even though I pass small ‘w’.

package com.techstackjournal;

public class SubstringMain {

	public static void main(String[] args) {

		String str = "Hello World Java";

		System.out.println(str.substring(str.toLowerCase().indexOf('w')));

	}

}
  • We want to find the substring starting from ‘w’, but in our source string “Hello World Java” we do not have that small case ‘w’
  • So to find the small ‘w’ character, we first convert the source string to lower case and call indexOf method call to find ‘w’
  • Since, we are finding the character on a string converted to lower case, we will get the index of ‘w’ even though it is not present in the source string
  • Using the above index we call the substring method with begin index argument
  • Calling toLowerCase doesn’t change the case of the source string, so the returned string would be “World Java”

Output:

World Java

Leave a Comment