Count the number of occurrence in a string

is there any api for counting the number of occurrence in a string or should i write a method myself?
for example. I want to count "." in "1.2.3.4.". i want to count the dot in this string.
I am just wondering.
Thanks in advance.

is there any api for counting the number of
occurrence in a string or should i write a method
myself?The latter.

Similar Messages

  • How do I count the number of occurrences of a string within a group of cells?

    Hello all, I'm trying to figure out how to count the number of times a child has completed certain tasks.  Here is a sample of the data (it is highly simplified here, but contains what I hope is needed to answer my question):
    Unnamed Table
    Objective
    John
    Ann
    Alex
    Dave
    Eric
    20a 20b 20c
    x
    20a 20b 20c
    x
    20a 20b 20c
    x
    x
    19b 20a
          x
           x
    20c 21b 22
         x
    What I am trying to do is count the number of times each child completed each objective - but I can't figure out how to go about splicing up the "objectives" fields for counting while still being able to compare them to whether or not the "child" has 'an x in the box' in that particular row.

    If I read your example correctly, John's counts should be 2 for 20a, 1 for 19b, 20b and 20c, and 0 for any other objectives listed.
    Here's an example that will work for your data set, assuming that any objective ma occur only once in each row of column A of the Main table. I've added an a to objective 22, but I think it is unnecessary, provided there is no objective 122, 221, etc.
    This uses a two step process.
    The table AUX extracts the objectives completed by each student, using the checkboxes in Main. Note that it also adds a space at the beginning and end of each string. This provides an extra character before the first objective code and after the last objective code in that row, used by the wildcard specification in the formula in the Summary table.
    Formula:
    Aux::B2: =IF(OFFSET(Data :: $A$1,ROW()-1,COLUMN()-1)," "&OFFSET(Data :: $A$1,ROW()-1,0)&" ","")
    Fill right to the last column and down to the last row of Aux.
    Summary uses COUNTIF to count the number of occurrences of each of the objectives listed in column A of that table.
    Formula:
    Summary::B2: =COUNTIF(Aux :: B,"=*"&$A2&"*")
    Fill right and down as the previous formula.
    Regards,
    Barry

  • How i can count the number of words in a string?

    hi, i want to know how to count the number of words in a string
    e.g. java is a very powerful computer language.
    i will get 7 words.
    thanks in advance..

    Jverd, this has actually been answered, but due to an
    attack of goldie-itis, all the answers were hosed.
    The OP did get an answer, though.Yeah, I know. I just didn't know if he saw the answer before it went away.

  • SSRS- Counting the number of occurrences

    Hi,
    I have table 1 filled with numbers 1-5. I need to count the number of occurrences for each number in table 2.
    For example,
    Table 1
    3
    4
    5
    4
    3
    Table 2 1-- 0 occurrences 
    2-- 0 occurrences
     3-- 2 occurrence
     4-- 2  occurrences
     5-- 1  occurrences
    Can I do this in a SSRS report?
    Thank you in advance!

    Hi Rebecca07,
    After testing the issue in my local environment, we can refer to the steps below to achieve your requirement:
    Drag a table from Toolbox to design surface.
    Delete the detail row (second row) and the third column.
    Right-click the first row to insert five rows after that row.
    Type the following expressions in each cell:
    numbers             occurrences
    1                        =count(iif(Fields!number.Value=1,1,nothing))
    2                        =count(iif(Fields!number.Value=2,1,nothing))
    3                        =count(iif(Fields!number.Value=3,1,nothing))
    4                        =count(iif(Fields!number.Value=4,1,nothing))
    5                        =count(iif(Fields!number.Value=5,1,nothing))
    The following screenshot is for your reference:
    If you have any other questions, please feel free to let me know.
    Thanks,
    Katherine Xiong
    Katherine Xiong
    TechNet Community Support

  • Using count to count the number of occurrences of a primary key

    Hi,
    I'm relatively new to T-SQL and more of a SAS guy.
    I just need to some help.
    I have a table
    CF_ID    Name
    4444     Mark
    4444    Mark
    3321   Sarah 
    All i want to do add in a new column to count the number of occurrence of the CF_ID. So my new table would be:
    CF_ID    Name     New_column
    4444     Mark       1
    4444     Mark       2
    3321     Sarah     1 
    Any help would be gratefully appreciated.
    Thank you
    Umar Javed

    Hello Umar,
    You can use the
    ROW_NUMBER (Transact-SQL) for this:
    SELECT *, ROW_NUMBER() OVER (PARTITION BY CF_ID ORDER BY Name) AS New_Column
    FROM yourTable
    Olaf Helper
    [ Blog] [ Xing] [ MVP]

  • Which method to count the number of character in a string?

    Hi all,
    I want to know which class and method to count the number of character in a string?
    Gary

    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

  • How count the number of substrings in a string

    Hi all,
    I wonder if someone can help me and point out where I'm going wrong.
    I want to count the number of substring starting with 'd' and ending with 'e' in a string supplied by a user.
    currently my code is only counting the number of 'd' and 'e' characters in the string.
        public static void main (String [] args)
             Scanner input = new Scanner (System.in);
             String mstring;// string variable that the user will enter
             int i = 0; // upstep variable
             int count = 0; // int variable to count the number of substrings
             System.out.print("Enter a string: ");
             mstring = input.nextLine();
             while (i != mstring.length())
                       if(mstring.charAt(i)=='d'){count++;}
                       else if (mstring.charAt(i)=='e'){count++;}
                       i++;
             System.out.println("The total d e substrings is " + count);
    }For example if the user enters adbedeaadffe the total number of substrings should be 5 but I'm getting 6 as the program is just counting the number of times it comes across 'd' and 'e'
    Can anyone shed some light on this for me thanks.

    Hi all I had a good few replies to this question but they seem to have been removed from the system for some reason.
    I've changed my code slightly and it works but only for one round of the guard.
                   Scanner input = new Scanner (System.in)
             String mstring;// main string variable that the user will enter
             int i = 0;  // upstep variable
             int count = 0; // int variable to count the number of substrings
         System.out.print("Enter a string: ");
             mstring = input.nextLine();
             int y =mstring.length();
             while (i != mstring.length() && y !=0)
             if (mstring.charAt(i) == 'd' && mstring.charAt(y-1)=='e'){count = count+1;}
             y--;
             i++;
             System.out.println("The total a b substrings is " + (count));The above code will only print out one substring of adbedeaadffe but that is not what I need, I need to be able to print out all of the substrings.
    Can anyone tell me where I'm going wrong.
    Thanks in advance

  • How do I count the number of *'s in a string?

    I need to count the number of * in a string of carakters. And the return the number. 0 for no *'s in the string 2 for two *'s in the string and so on.
    It's probably quite simple but I can't figure out how!

    Like this.
    james_t_dk wrote in message
    news:[email protected]..
    > I need to count the number of * in a string of carakters. And the
    > return the number. 0 for no *'s in the string 2 for two *'s in the
    > string and so on.
    >
    > It's probably quite simple but I can't figure out how!
    [Attachment Untitled 10.vi, see below]
    Attachments:
    Untitled_10.vi ‏18 KB

  • A quick way to count the number of  newlines '/n' in string of 200 chars

    I am trying to establish the number of lines that a string will generate.
    I can do this by counting the number of '/n' in the string. However my brute force method (shown below) is very slow.
    Normally this would not be a problem on a 2800mhz Athlon (Standard) PC this takes < 1 second. However this code resides within a speed critical loop (not shown). The code shown below is a Achilles heal as far as the performance of this speed critical loop goes.
    Can anyone suggest a faster way to count the number of �/n� (new lines) within a text string of around 50- 1000 chars, given that there may be 10 � 100 newline chars. Speed is a very important factor for this part of my program.
    Thanks in advance
    Andrew.
        int lineCount =0;
        String txt = this.getText();
        //loop throught text and count the carridge returns
        for (int i = 0; i < txt.length(); i++)
          char ch = txt.charAt(i);
          if (ch == '\n')
           lineCount ++;
        }//end forMessage was edited by:
    scottie_uk
    Message was edited by:
    scottie_uk

    Well, here is a C version. On my computer the Java version (reply 9 above) is slightly faster than C. YMMV. For stuff like this a compiler can be hard to beat even with assembler, as you need to do manual loop unrolling and method inlining which turn assembly into a maintenance nightmare.
    // gcc -O6 -fomit-frame-pointer -funroll-loops -finline -o newlines.exe newlines.c
    #include <stdio.h>
    #include <string.h>
    #if defined(__GNUC__) || defined(__unix__)
    #include <time.h>
    #include <sys/time.h>
    #else
    #include <windows.h>
    #endif
    #if defined(__GNUC__) || defined(__unix__)
    typedef struct timeval TIMESTAMP;
    void currentTime(struct timeval *time)
        gettimeofday(time, NULL);
    int milliseconds(struct timeval *start, struct timeval *end)
        int usec = (end->tv_sec - start->tv_sec) * 1000000 +
         end->tv_usec - start->tv_usec;
        return (usec + 500) / 1000;
    #else
    typedef FILETIME TIMESTAMP;
    void currentTime(FILETIME *time)
        GetSystemTimeAsFileTime(time);
    int milliseconds(FILETIME *start, FILETIME *end)
        int usec = (end->dwHighDateTime - start->dwHighDateTime) * 1000000L +
         end->dwLowDateTime - start->dwLowDateTime;
        return (usec + 500) / 1000;
    #endif
    static int count(register char *txt)
        register int count = 0;
        register int c;
        while (c = *txt++)
         if (c == '\n')
             count++;
        return count;
    static void doit(char *str)
        TIMESTAMP start, end;
        long time;
        register int n;
        int total = 0;
        currentTime(&start);
        for (n = 0; n < 1000000; n++)
         total += count(str);
        currentTime(&end);
        time = milliseconds(&start, &end);
        total *= 4;
        printf("time %ld, total %d\n", time, total);
        fflush(stdout);
    int main(int argc, char **argv)
        char buf[1024];
        int n;
        for (n = 0; n < 256 / 4; n++)
         strcat(buf, "abc\n");
        for (n = 0; n < 5; n++)
         doit(buf);
    }

  • Count the number of char in a string

    hi gurs
    for example ,there is a string "abcdaa", i want a sql that shows the number of 'a' in the string ,in this case,the sql should return 3 cause there are three 'a' in the string, how to write this sql
    thank you very much.

    As an alternative or if you're using 9i or below you can use this:
    SQL> with t as (
      2  select 'abcdaa' x from dual union all
      3  select 'AbcdaA' x from dual)
      4  -- end of sample data
      5  SELECT x, length(x) - length(replace(x, 'a', '')) count_chr
      6    FROM t;
    X       COUNT_CHR
    abcdaa          3
    AbcdaA          1
    SQL>

  • 'COUNT' the number of occurrences of a characteristic in BEx

    Good day all.
    3.x BEX.
    I want to count all the occurrences of a " Claim Number" in a BEx query.
    The u201CClaim numberu201D might have several u201Clinesu201D ie : Claim number u201CMMC123u201D has line 1, 2 and 3. I only want to count it as u20181u2019 Dealer claim number, not u20183u2019.
    I have a InfoObject "Counter" in DSO and transformation that counts all the lines, not just the claim numbers. If I set the counteru2019s key figure properties (calculate single value as..) to u2018minimumu2019, I will receive the correct quantity of lines when u201Cclaim numberu201D is displayed in the query results. BUT, I only want to display the total in the query (no detail).
    If I remove the Dealer claim number from the result, then the total displayed includes all the lines for the claim number as well.
    How do I get the correct quantity to display?
    Thanks in advance.
    Cj

    Hi,
    Use exception aggregation for Keyfigure (counter) Properties with referce to the char Claim Number.
    Goto->Counter(KF)Properties->exception aggregationTab-->select refchar(Claim Number)-->ok.
    then u will get aggregation value.
    Hope it willhelp.
    Regards,
    Satya

  • Count the number of unique values in column

    Hi
    I have a large numbers file (thousands of rows) with data for different dates. I need to calculate the number of unique (distinct) dates are in the spreadsheet.
    Does anyone know how to do this?
    Thanks,
    Bruce

    bpj,
    Let's say your date is in column B for this example and your first data row is Row 2. In another column you can enter into row 2: =IF(COUNTIF($B$2:B2, B)=1, 1, "") Summing the new column content will yield the number if unique dates.
    As badunit observes, this will result in sluggish document. If the number of possible dates is small compared to the number of rows of data, it would be better to make a table of possible dates and count the number of occurrences of each of those dates in the data, a COUNTIF function on the frequency data for the date range will give you the number of unique dates, possible much more efficiently.
    Jerry

  • Count the number of times a character is in a string using pl/sql

    I need to count the number of times ":" appers in the string ":XXX:CCC:BBB:".
    I have sound some solution using SQL but I do not want the context switch.
    Also I am on 10g so I can not use REGEXP_COUNT.
    Any help would be great.

    Hi,
    length(REGEXP_REPLACE(':XXX:CCC:BBB:','[[:alnum:]]'))counts all kinds of punctuation, spaces, etc., not just colons. Change any (or all) of the colons to periods and it will still return 4. Use '[^:]' instead of '[[:alnum:]]' if you really want to count just colons.
    Also, "SELECT ... FROM dual" is usually needed only in SQL*Plus or similar front end tools. In PL/SQL, you can call functions without a query, like this:
    x := NVL (LENGTH (REGEXP_REPLACE (txt, '[^:]')), 0);

  • I need to WAP to count the number of occurences of an alphabet in a string.

    I need to WAP to count the number of occurences of an alphabet in a string.I tried a lot and have surfed a lot regarding this problem.
    I m not the most proficient with java.but this is all i could come up with,and would appreciate some help here.I hope you guys would help me find a solution to this.
    e.g String : abcabrty
    Result should be
    a:2
    b:2
    c:1
    r:1
    t:1
    y:1
    public class chkoccurences
         public static void main(String args[ ])
              String user_Data=args[0];
              int counter=0;     
              try
                   for(int i=0;i<user_Data.length( );i++)
                        for(int j=0;j<user_Data.length( );j++)
                             if(user_Data.charAt(i) == user_Data.charAt(j))
                             counter++;
                        System.out.println(user_Data.charAt(i)+" exists "+counter+" time(s) in the word.");
                   System.out.println(" ");
              catch(ArrayIndexOutOfBoundsException e)
                   System.out.println("Check the array size.");
    }This is the output i get out of the program:
    a exists 2 time(s) in the word.
    b exists 4 time(s) in the word.
    c exists 5 time(s) in the word.
    a exists 7 time(s) in the word.
    b exists 9 time(s) in the word.
    r exists 10 time(s) in the word.
    t exists 11 time(s) in the word.
    y exists 12 time(s) in the word.What i think is i need an array to store the repeated characters because the repeated characters are getting counted again in the loop,if you know what i mean.
    Please, i would appreciate some help here.

    Criticism is welcomed
    public class tests {
         final int min = 10;
         final int max = 35;
         final char[] chars = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
                   'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
                   'v', 'w', 'x', 'y', 'z'};
         public static void main(String[] args){
              tests t = new tests();
              String[] strings = new String[] {"aabcze", "att3%a", ""};
              for(String s : strings){
                   System.out.println(t.getAlphaCount(s));
         public String getAlphaCount(String s){
              int[] alphaCount = new int[26];
              int val;
              for(char c : s.toCharArray()){
                   val = Character.getNumericValue(c);
                   if( (val>=min) && (val<=max)){
                        alphaCount[val-min]++;
              StringBuilder result = new StringBuilder();
              for(int i=0; i<alphaCount.length; i++){
                   if(alphaCount[i] > 0){
                        result.append(chars[i] + ":" + alphaCount[i] + ", ");
              if(result.length() != 0){
                   result.delete(result.length()-2, result.length());
              return result.toString();
    }

  • Fastest way to count the number of occurences of string in file

    I have an application that will process a number of records in a plain text file, and the processing takes a long time. Therefore, I'd like to first calculate the number of records in the file so that I can display a progress dialog to the user (e.g. " 1234 out of 5678 records processed"). The records are separated by the string "//" followed by a newline, so all I need to do to get the number of records is to count the number of times that '//' occurs in the file. What's the quickest way to do this? On a test file of ~1.5 Gb with ~500 000 records, grep manages under 5 seconds, whereas a naive Java approach:
    BufferedReader bout = new BufferedReader (new FileReader (sourcefile));
                   String ffline = null;
                   int lcnt = 0;
                   int searchCount = 0;
                   while ((ffline = bout.readLine()) != null) {
                        lcnt++;
                        for(int searchIndex=0;searchIndex<ffline.length();) {
                             int index=ffline.indexOf(searchFor,searchIndex);
                             if(index!=-1) {
                                  //System.out.println("Line number " + lcnt);
                                  searchCount++;
                                  searchIndex+=index+searchLength;
                             } else {
                                  break;
                   }takes about 10 times as long:
    martin@martin-laptop:~$ time grep -c '//' Desktop/moresequences.gb
    544064
    real     0m4.449s
    user     0m3.880s
    sys     0m0.544s
    martin@martin-laptop:~$ time java WordCounter Desktop/moresequences.gb
    SearchCount = 544064
    real     0m42.719s
    user     0m40.843s
    sys     0m1.232sI suspect that dealing with the file as a whole, rather than line-by-line, might be quicker, based on previous experience with Perl.

    Reading lines is very slow. If your file has single byte character encoding then use something like the KMP algorithm on an BufferedInputStream to find the byte sequence of "//\n".getBytes(). If the file has a multi-byte encoding then use the KMP algorithm on a BufferedReader to find the chars "//\n".getCharacters() .
    The basis for this can be found in reply #12 of http://forum.java.sun.com/thread.jspa?threadID=769325&messageID=4386201 .
    Edited by: sabre150 on May 2, 2008 2:10 PM

Maybe you are looking for