![]() ![]() We’ve passed character d which is the last letter of desired string as an argument to indexOf method to get the index.In the second argument, we are calling indexOf method to return the index of the desired character.We’ve used substring method and passed 0 as the first argument, as we want to read the String from start.We want to read “Hello World” from “Hello World Java”.In this example, we want to return a substring from start till a given character. Output: World Java Get Substring Which is Before a Character in Java The returned value will be “World Java”.Once we receive the index from indexOf method, the substring method will use that index as the begin index.The indexOf function will return the index of the given character.In this example, we will read a substring of a String from a given character till the end using the substring and indexOf functions. Get Substring Which is After a 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. In this example, we’ll look into examples on substring based on a given character in Java. String class provides us with powerful String processing methods, out of those, substring is one. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |