 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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 index of the first occurrence of a separator in Java
We have the following string with two separators.
String str = "Tom-Hank-s";
We want the index of the first occurrence of the separator.
For that, you need to get the index of the separator using indexOf()
String separator ="-"; int sepPos = str.indexOf(separator);
The following is an example.
Example
public class Demo {    public static void main(String[] args) {       String str = "Tom-Hank-s";       String separator ="-";       System.out.println("String: "+str);       int sepPos = str.indexOf(separator);       System.out.println("Separator's first occurrence: "+sepPos);    } }  Output
String: Tom-Hank-s Separator's first occurrence: 3
Advertisements
 