new to java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rotaryfreak
    New Member
    • Oct 2008
    • 74

    new to java

    i was just wondering if there was a method in java that would return the number of occurrences of a letter. for example, if the string was:

    String hello = "Hello World";

    id want a function that returns the occurrences of the letter "l" and would return an integer value of 3

    any suggestions?

    thanks
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    No, you may create your own specific character occurence counter......

    sample method without loop:

    Code:
    private int occurs(String data,char find){
    		return data.length()-data.replaceAll(find+"","").length();
    	}

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by sukatoa
      No, you may create your own specific character occurence counter......

      sample method without loop:

      Code:
      private int occurs(String data,char find){
      		return data.length()-data.replaceAll(find+"","").length();
      	}
      That is intimidating for the OP; it assumes knowledge of regular expressions.
      Better do it the naive way first:

      Code:
      private in occurs(String data, char find) {
      
         int count= 0;
         for (int i= 0; i < data.length(); i++)
            if (data.charAt(i) == find)
               count++;
         return count;
      }
      When the OP feels a bit more comfortable we can bring in the trickery-dickery again ;-)

      kind regards,

      Jos

      Comment

      • rotaryfreak
        New Member
        • Oct 2008
        • 74

        #4
        Thanks Jos for your quick response, i appreciate it!





        Originally posted by JosAH
        That is intimidating for the OP; it assumes knowledge of regular expressions.
        Better do it the naive way first:

        Code:
        private in occurs(String data, char find) {
        
           int count= 0;
           for (int i= 0; i < data.length(); i++)
              if (data.charAt(i) == find)
                 count++;
           return count;
        }
        When the OP feels a bit more comfortable we can bring in the trickery-dickery again ;-)

        kind regards,

        Jos

        Comment

        • chaarmann
          Recognized Expert Contributor
          • Nov 2007
          • 785

          #5
          Jos solution is the fastest.
          But if you don't care for the execution speed but only for the brevity of code, you can use:

          Code:
          int numberOfOccurrences = ("<" + "Hello World" + ">").split("l").length - 1;
          You need to surround your original string by some characters that you are not searching for (I used "<" and ">"), if it may happen that a searchable character can be the first and/or last character of the string. If this cannot happen by the nature of your string, you can make this line even shorter:

          Code:
          int numberOfOccurrences = "Hello World".split("l").length - 1;

          Comment

          Working...