AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
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 ?.



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 351
programming x 167
Posted On : 2018-03-22 20:07:45.0
profile pranesh - anyforum.in pranesh
100
up-rate
1
down-rate

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
profile Rishi Kumar - anyforum.in Rishi Kumar
523187021394
Reply This Thread
up-rate
0
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in