

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- 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?
- How is it possible in MySQL to find the location of the first occurrence of a substring in a string?
- Replace first occurrence of a character in Java
- How to get the first index of an occurrence of the specified value in a string in JavaScript?
- Get the substring of a column in MySQL
- How to find the nth occurrence of substring in a string in Python?
- Remove the first occurrence of a specific object from the ArrayList in C#
- Remove the first occurrence from the StringCollection in C#
- Display the position of a Substring in Java
- How to get the first element of the List in Java?
- C# program to remove the first occurrence of a node in a Linked List
Advertisements