如何判断一个字符串里含有,逗号

解决方案 »

  1.   

    String类中有方法indexOf();
    indexOf 
     public int indexOf(String str)返回指定子字符串在该字符串中第一次出现处的索引。 参数: 
    str - 子字符串。 
    返回: 
    若字符串参数作为该对象的子串出现,则返回第一个这样的子串第一个字符的索引,如果它没有作为一个子串出现则返回 -1 。
      

  2.   

    String test = "12321,asfd";String [] result = test.split( "," ) ;if( result.length > 1 )
    {
        有逗号
    }
      

  3.   

    try {
    Pattern regex = Pattern.compile("^[^,]*$",
    Pattern.CANON_EQ);
    Matcher regexMatcher = regex.matcher(subjectString);
    if (regexMatcher.find()) {
    // regexMatcher.group(); regexMatcher.start(); regexMatcher.end();
    } else {


    } catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
    }
      

  4.   

    if(str.indexOf(",") >= 0)
        System.out.println("字符串中有逗号");
      

  5.   

    true表示没有,false表示有逗号
      

  6.   

    public class GetString {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            String str = "12hhgdfr3,sasdf456";
                int isComma = (str.indexOf(","));
                if(isComma > 0) 
                    System.out.println("It has");
                else 
                    System.out.println("It doesn't have");
           }
    }
      

  7.   

    将程序改了下:public class GetString {    public static void main(String[] args){            String str = "12hhgdfr3,sasdf456";
                int isComma = (str.indexOf(","));
                if(isComma == -1)
                  System.out.println("It doesn't have");
                else 
                  System.out.println("It has");         }}
      

  8.   

    String [] result = test.split( "\\," ) ;
      

  9.   

    String a = "laskdjfls,;aslkdjflkj";
    if(a.indexOf(',') != 0)
    System.out.println("It has one ',' index of "+a.indexOf(','));