- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get the substring after the first occurrence of a separator in Java
We have the following string with a separator.
String str = "Tom-Hanks";
We want the substring after the first occurrence of the separator i.e.
Hanks
For that, first you need to get the index of the separator and then using the substring() method get, the substring after the separator.
String separator ="-"; int sepPos = str.indexOf(separator); System.out.println("Substring after separator = "+str.substring(sepPos + separator.length()));
The following is an example.
Example
public class Demo { public static void main(String[] args) { String str = "Tom-Hanks"; String separator ="-"; int sepPos = str.indexOf(separator); if (sepPos == -1) { System.out.println(""); } System.out.println("Substring after separator = "+str.substring(sepPos + separator.length())); } }
Output
Substring after separator = Hanks
- Related Articles
- Get the substring before the last occurrence of a separator in Java
- Get the index of the first occurrence of a separator in Java
- Get the first occurrence of a substring within a string in Arduino
- Get the last occurrence of a substring within a string in Arduino
- How to update a value with substring of current value by removing the separator and numbers after a separator in MySQL?
- Replace first occurrence of a character in Java
- How is it possible in MySQL to find the location of the first occurrence of a substring in a string?
- How to get the first index of an occurrence of the specified value in a string in JavaScript?
- How to find the nth occurrence of substring in a string in Python?
- Get the substring of a column in MySQL
- Remove the first occurrence of a specific object from the ArrayList in C#
- Remove the first occurrence from the StringCollection in C#
- PHP – iconv_strpos() function – Find the position of the first occurrence of a needle in a haystack
- Count all elements in the array which appears at least K times after their first occurrence in C++
- Display the position of a Substring in Java

Advertisements