나는 String ("Dinosaur")을 가지고 있는데 정확히 어떻게 모르겠지만 char "o"의 위치를 얻는 방법은 무엇입니까? 내 문자열 ("풀")처럼 두 위치를 얻을 수있는 모든숯의 위치를 알아내는 방법
답변
첫 번째 질문은 String#indexOf(int)을 사용하여 문자열의 모든 'o'색인을 가져올 수 있습니다. 두 번째 질문에 대해서는
int oPos = yourString.indexOf('o');
, String.indexOf(int, int)를 사용하는 방법을 당신이 문자열의 일부를 검색을 반복하지 않도록 이전 인덱스를 추적하여 해당 문자의 모든 위치를 얻을 수 있습니다. 배열이나리스트에 위치를 저장할 수 있습니다. 루프와
사용 indexOf
: 간단하게
String s = "Pool";
int idx = s.indexOf('o');
while (idx > -1) {
System.out.println(idx);
idx = s.indexOf('o', idx + 1);
}
:
public static int[] getPositions(String word, char letter)
{
List<Integer> positions = new ArrayList<Integer>();
for(int i = 0; i < word.length(); i++) if(word.charAt(i) == letter) positions.add(i);
int[] result = new int[positions.size()];
for(int i = 0; i < positions.size(); i++) result[i] = positions.get(i);
return result;
}
이 아마 헤이 보드 위에 조금가는 있지만)
String master = "Pool";
String find = "o";
Pattern pattern = Pattern.compile(find);
Matcher matcher = pattern.matcher(master);
String match = null;
List<Integer[]> lstMatches = new ArrayList<Integer[]>(5);
while (matcher.find()) {
int startIndex = matcher.start();
int endIndex = matcher.end();
lstMatches.add(new Integer[] {startIndex, endIndex});
}
for (Integer[] indicies : lstMatches) {
System.out.println("Found " + find + " @ " + indicies[0]);
}
나에게
를 제공합니다Found o @ 1
Found o @ 2
좋은 점은 "oo"도 찾을 수 있다는 것입니다.
맞습니다. 이것은 매우 외판입니다. – HXCaine
A 예, 그렇지만 매우 유연합니다.) – MadProgrammer
사람들은 가끔 배 밖으로 빠져들지 만, "헤이"는 용서할 수 없습니다 :-) – paxdiablo
String을 char 배열로 변환 해 보았습니까?
int counter = 0;
String input = "Pool";
for(char ch : input.toCharArray()) {
if(ch == 'o') {
System.out.println(counter);
}
counter += 1;
}
문자열의 문자를 대체 난 단지 코드가 발견 한이
String s= "aloooha";
char array[] = s.toCharArray();
Stack stack = new Stack();
for (int i = 0; i < array.length; i++) {
if(array[i] == 'o'){
stack.push(i);
}
}
for (int i = 0; i < stack.size(); i++) {
System.out.println(stack.get(i));
}
- 1. 숯의 증분
- 2. 트위터로 위치를 알아내는 중 트위터 검색 API
- 3. 숯의 끝 부분 잘라 내기 *
- 4. 숯의 끝 부분 다듬기 *
- 5. 마우스 버튼의 위치를 알아내는 부울 함수
- 6. DirectX 내 카메라를 넣어야하는 위치를 알아내는 중
- 7. 안드로이드 3.0에서 StackView의 위치를 알아내는 방법 .OnItemClick은 매번 0을 반환합니다.
- 8. Html5 끌어서 놓기 위치에서 드롭 위치를 알아내는 방법
- 9. 안드로이드의 화면과 관련하여 listview에서 Clicked Item의 y 위치를 알아내는 방법
- 10. 언젠가는 평일인지를 알아내는 방법
- 11. 모델명을 알아내는 방법
- 12. 블루투스가 끊긴시기를 알아내는 방법
- 13. 사용자 로그인을 알아내는 방법
- 14. 리디렉션 URL을 알아내는 방법
- 15. 주어진 IP로 셀룰러 공급자를 알아내는 방법?
- 16. HDFS 어떤 호스트로부터 파일을 얻는지를 알아내는 방법
- 17. 문자열의 문자가 정수인지 알아내는 방법
- 18. MySQL에서 색인의 크기를 알아내는 방법
- 19. PDA가 인식하는 언어를 알아내는 방법
- 20. 주간 시작 날짜를 알아내는 방법
- 21. JDialog가 표시되는 화면을 알아내는 방법
- 22. 함수의 호출자 클래스를 알아내는 방법
- 23. who is FirstResponder를 알아내는 방법?
- 24. 내 "openid"를 알아내는 방법
- 25. 사용자 MySite의 ID를 알아내는 방법
- 26. 순환 의존성이 어디인지 알아내는 방법
- 27. 어떤 방법으로 'exe'크기를 늘리는 지 알아내는 방법
- 28. Solr의 각 필드가 차지하는 공간을 알아내는 방법
- 29. 브라우저 플러그인에서 플래시 스트림 URL을 알아내는 방법
- 30. 자바 프로그램이들을 수있는 인터넷 주소를 알아내는 방법
가, 지금은이 코드를 사용하려는 시도하지만 난 "t의 사용은 난의 위치를 찾을 수 없습니다 수있는 경우 char –
[documentation] (http://docs.oracle.com/javase/7/docs/api/java/lang/String.html)은 친구입니다. 찾고있는 방법은'indexOf'입니다. – Jeffrey