
- 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 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
- Related Questions & Answers
- Get the substring after the first occurrence of a separator in Java
- Get the substring before the last occurrence of a separator in Java
- How to get the first index of an occurrence of the specified value in a string in JavaScript?
- Index of first occurrence in StringCollection in C#?
- Get the first occurrence of a substring within a string in Arduino
- Replace first occurrence of a character in Java
- How to get the last index of an occurrence of the specified value in a string in JavaScript?
- Python - Get the Index of first element greater than K
- Python Pandas - Indicate duplicate index values except for the first occurrence
- Remove the first occurrence of a specific object from the ArrayList in C#
- Python Pandas - Return Index with duplicate values removed except the first occurrence
- How to find the index of the last occurrence of repeated values in a vector in R?
- Get the index of a particular element in an ArrayList in Java
- Get the last occurrence of a substring within a string in Arduino
- First occurrence of True number in Python
Advertisements