AF
Home
Tag
Submit Notes
Ask Anything
Login
Subscribe Us
A
ny
F
orum
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.
Follow @anyforumin
corejava-programming: indexOf character method in java
find charcter in the string if character mathches in string characters print character position else print -1
corejava x 348
programming x 165
Posted On :
2018-03-22 20:07:45.0
pranesh
1
0
0
1
Answers
You can use indexOf method of String class to find the character index in a String. If position here starts from 1 you can place the condition if index!=-1, position = index+1 like below code snippet otherwise just use indexOf.
public class StringUtils {
public static int getCharacterPosition(String input, char ch){
int index=input.indexOf(ch);
int position= index!=-1?index+1:index;
return position;
}
public static void main(String[] args) {
String input="I Love You!";
System.out.println(getCharacterPosition(input,'L'));
System.out.println(getCharacterPosition(input,'!'));
System.out.println(getCharacterPosition(input,'K'));
}
}
Posted On :
2018-03-22 22:35:13
Satisfied :
0 Yes
0 No
Rishi Kumar
523
1862
20593
Reply This Thread
0
Post Answer
Please Login First to Post Answer:
Login
Answer:
anyforum.in