2012-03-27 9 views
1

누군가이 절에서 어떻게이 절에서 십진수 분리자를 얼마나 자주 사용했는지를 말할 수 있습니까?자바에서 문자열의 문자 발생 횟수를 계산하는 방법

예 : 1234,56,789

+0

','의 발생을 계산 하시겠습니까? –

+0

문자열에서','의 발생 횟수를 계산 하시겠습니까? – beerbajay

+0

해당 문자열의 소수 구분 기호는 어디에 있습니까? –

답변

5

만큼 간단한 : 당신은 그냥 내가 simpliest 방법으로 생각

String s = "1234,56,78"; 
System.out.println(s.split(",").length); 
+1

빠른 답변 감사합니다. 그게 전부 나를 위해 :) – FabianG

+1

나는 아래의 API 및 편리한 도구 CountMatches 메서드를 사용 해왔다. http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html –

9
String number = "1234,56,789"; 
int commaCount = number.replaceAll("[^,]*", "").length(); 
+0

이것은 훌륭한 해결책입니다. –

1

String.split(",")을 exucute하고 배열의 크기를 계산하는 것입니다.

그래서 명령은 다음과 같이 잡아 줄께 :

String s = "1234,56,789"; 
int numberofComma = s.split(",").length; 

감사를 에릭

4

를 사용 (있는 경우) 절을 필요로하지 않는

String number = "1234,56,789"; 
int count = 0; 
for (int i = 0; i < number.length(); i++) 
    if (number.charAt(i) == ',') 
     count++; 
// count holds the number of ',' found 
2

을 당신이 아닌 경우 절을 사용할 수 있다면, 당신은 할 수 있습니다 :

int count = number.split(",").length 
1
public class OccurenceOfChar { 

public static void main(String[] args) throws Exception { 
    // TODO Auto-generated method stub 

    BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
    System.out.println("Enter any word"); 
    String s=br.readLine(); 

    char ch[]=s.toCharArray(); 
    Map map=new HashMap(); 


    for(int i=0;i<ch.length;i++) 
    { 
     int count=0; 
     for(int j=0;j<ch.length;j++) 
     { 
      if(ch[i]==ch[j]) 
       count++; 
     } 
    map.put(ch[i], count); 


    } 
    Iterator it=map.entrySet().iterator(); 
    while(it.hasNext()) 
    { 
     Map.Entry pairs=(Map.Entry)it.next(); 
     System.out.println("count of "+pairs.getKey() + " = " + pairs.getValue()); 
    } 





    } 
} 
관련 문제