Count Comma's from a String

Hi, Do I know which command in Processing will allow me to count comma's from a string?

Answers

  • What have you tried? You might take a look at the Processing reference or the Java API (or JavaScript, depending on which mode you're using) for useful functions.

  • I have a long string like GPS= "78,54,675,24,426,52,6435,452," want to count "COMMAs" inside it.

  • What have you tried? You might take a look at the Processing reference or the Java API (or JavaScript, depending on which mode you're using) for useful functions.

  • There are lot of ways to do that, from looping over each character of the string, to a method I like, which is a bit overkill, but works well and is quite concise:

     String GPS = "78,54,675,24,426,52,6435,452,";
     String cl = GPS.replaceAll("\\d", "");
     print(cl.length());
     exit();
    
  • static final int countChar(CharSequence s, char c) {
      int n = 0, i = s.length();
      while (i-- != 0)  if (s.charAt(i) == c)  ++n; 
      return n;
    }
    
Sign In or Register to comment.