Count the occurence of a character in a string

Post Author: halfpat
CA Forum: Formula
Hello to everyone,
I use CR 8,0. Is anyone can tell me if this is possible:
I have a string "12345-C-1 23456-C-2 34567-C-4", how can I count the occurence of the "C" character in this string ?Is the InStr function can help me to do this ?
I appreciate any suggestion,
Thanks.

Post Author: bettername
CA Forum: Formula
How about measuring the length of the string, and subtracting the length of the same string where C is replaced with <nothing>?
len("12345-C-1 23456-C-2 34567-C-4") - len(replace("12345-C-1 23456-C-2 34567-C-4" , "C", ""))

Similar Messages

  • Count the occurance of a character in a string

    What is the function to count the occurance of a character in a string.
    like 'test test1 test2 test3' with in this string there are 3 white spaces.
    instr will give the first one, replace will change the all... and what's for the occurance (number of white spaces, which is 3 in this case) count
    Message was edited by:
    gladnn

    SQL> var a varchar2(25)
    SQL> exec :a := 'gghhhh999jjjj'
    &nbsp
    PL/SQL procedure successfully completed.
    &nbsp
    SQL> select length(:a) - nvl(length(replace(:a,'h','')),0) from dual;
    &nbsp
    LENGTH(:A)-NVL(LENGTH(REPLACE(:A,'H','')),0)
                                               4Rgds.

  • How to count number of occurences of a character  in a string

    I want to get the count of occurences of a character within a string. Is there any function for this or should i go for a PLSQL.
    For example is the sting is "occurences" & if i search for character "c", the answer should be 3.
    Regards,
    Sunil.

    SQL> select length('occurence') - length(replace('occurence','c')) from dual;
    LENGTH('OCCURENCE')-LENGTH(REPLACE('OCCURENCE','C'))
    3

  • Count the occurences of a specific combination of chars

    Hi there
    I want to count the occurences of a specific combination of characteristic in a query.
    Example count the occurences of combination CHAR1 CHAR2 CHAR3
    CHAR1 CHAR2 CHAR3 Counter
    A     BB     CCC    1
    A     BB     CCC    1
    A     BB     DDD    1
    A     SS     DDD    1
    Query Result should be
    CHAR3 Count
    CCC    1
    DDD    2
    In the HowTo Paper: Count the occurrences of a
    characteristic relative to one or
    more other characteristics it is only a solution with one characteristic but not with a combination of chars.
    I don't know if this possible, because I think it is necessary to do a expeption aggregation based on several chars.
    Anyone an Idea?
    Thanks a lot in advance
    Joe

    Joe,
    I think in your case it is the case of multiple characteristics and I think the straight way is to do with exception aggregation.
    See the below link
    http://help.sap.com/saphelp_nw04/helpdata/en/d2/e0173f5ff48443e10000000a114084/frameset.htm
    See the below link for some more discussions using formulas which may help you to arrive a decision.
    Count elements in a characteristic
    Regs
    Gopi.

  • Count the no.of of numericals from a string.

    Hi team
    I would like to count the no.of numericals from a given string.
    Ex.
    String - '1,23,567 8-9'
    The result should be 5.
    Eliminate the comma and space and hypen (-) while counting.
    1 - is 1
    23 - is 2
    567 - is 3
    8 - is 4
    9 - is 5
    Kindly help me.
    Regards
    Nakul Venkatraman

    Just one suggestion. Your solution implies single character separators:
    select length(regexp_replace('1, 23, 567 8-9','(^|\D)(\d)+','*')) str from dual
           STR
             7Small adjustment makes it separator length independent:
    select length(regexp_replace('1, 23, 567 8-9','(^|\D+)(\d)+','*')) str from dual
           STR
             5
    SQL> And it will work for 10g, but for 11g regexp_count is still simpler solution.
    SY.

  • How to check the occurrence of a character in a string

    Hello Experts,
    I have a scenario where in I have to check the occurrence of a character in a string and based on that I have to pass a data into target field.
    I was wondering whether this can achieved with mapping functions or Do I have to use an UDF.
    Thanks in advance.
    Regards
    Advit Ramesh

    Hi Advit,
    You can achieve this by using standard function indexOf from the text category. Pass in the input string and the character value you want to check for as the input.
    Standard Functions (SAP Library - SAP Exchange Infrastructure)
    If the output is -1, then the character is not in the string, otherwise it is.
    Rgds
    Eng Swee

  • Counting the number of occurences in a table column

    Hi All
    I have a table with a column that contains approx. 5000 6-digit codes. A number of these codes are duplicted in the column, and I want to count the number of occurences of each code. The column looks a bit like -
    WCID
    940042
    920012
    940652
    940199
    188949
    155146
    155196
    174196
    152148
    151281
    196209
    174015
    182163
    195465
    195318
    182008
    189589
    150675
    There can be mulitple instances of each WCID and I need to count the number of instances of each. I also have access to another table that also has a column of each WCID, but only once - ie no multiple instances. The second table is identical except that there are only single instances of each WCID.
    I thought I could either loop through on the table to be counted, from 100000 to 999999 and count each occurence that way, but it would be very inefficient. The other way I thought would be to perhaps select a WCID from the unique table, count the occurence of that, select the next WCID from the unique table, count that and so on, however I'm not sure how to do it in PL/SQL. Perhaps select the WCID from the unique table into a cursor, loop through that and compare it with the original table and count the instances?
    I hope this makes some sense, any help would be really appreciated
    Thanks
    Bill

    Hi, Bill,
    That sounds like a job for GROUP BY:
    SELECT    wcid
    ,         COUNT (*)       AS num_found
    FROM      table_x
    GROUP BY  wcid
    ORDER BY  wcid
    I hope that answers your question.
    If not, post CREATE TABLE and INSERT statements for a little sample data, and the results you want from that data.

  • How to count a occurence of a string in file

    import java.util.*;
    import java.io.*;
    class Count
    public static void main(String args[ ] )throws Exception
         FileReader fr=new FileReader("d:\\compfetch\\clean\\first.txt");
         BufferedReader br=new BufferedReader(fr);
         StringBuffer sb=new StringBuffer();
         while((s=br.readLine())!=null)
              sb.append(s);
         s=sb.toString();
    Vector v= new Vector();
         v.addElement("job");
         v.addElement("jobs");
         v.addElement("career");
         v.addElement("careers");
         v.addElement("vacancy");
         v.addElement("vacancies");
         v.addElement("opportunity");
         v.addElement("openings");
         v.addElement("posting");
         v.addElement("postings");
         v.addElement("opportunities");
         v.addElement("placement");
         v.addElement("placements");
         v.addElement("job");
         v.addElement("job");
         Enumeration vEnum=v.elements();
         while(vEnum.hasMoreElements())
              System.out.println(vEnum.nextElement());
    I need to count the occurence of each string given in the vector from the text file.I have the file and the vector.How do i compare the vector and textfile ,so that i can count the occurence of eachString in the vector with the text file.The text file may contain any of the words given in the vector.If found ,a count shld be made for each string .
    regards,
    koel

    Try this code:
    import java.util.*;
    import java.io.*;
    class Count
    public static void main(String args[ ] )throws Exception
         FileReader     fr = new FileReader("c.c");
         BufferedReader br = new BufferedReader(fr);
         StringBuffer   sb = new StringBuffer();
         String         s;
         while((s=br.readLine())!=null)
              sb.append(s);
         s=sb.toString();
         Vector v= new Vector();
         v.addElement("job");
         v.addElement("jobs");
         v.addElement("career");
         v.addElement("careers");
         v.addElement("vacancy");
         v.addElement("vacancies");
         v.addElement("opportunity");
         v.addElement("openings");
         v.addElement("posting");
         v.addElement("postings");
         v.addElement("opportunities");
         v.addElement("placement");
         v.addElement("placements");
         Enumeration vEnum=v.elements();
         while(vEnum.hasMoreElements())
              String l = (String)vEnum.nextElement();
              int    a = 0;
              int    p = s.indexOf(l,a);
              int    c = 0;
              while (p != -1)
                   c = c + 1;
                   a = a + l.length() + p;
                   p = s.indexOf(l,a);
              System.out.println(l+"  "+c);
    }

  • PL/SQL - counting values' occurences in a collection - how to optimize?

    Hi all,
    I'm new here and working currently on my PhD thesis, using Oracle (XE) to do some data manipulation stuff.
    Following is defined:
    TYPE stat_col_type IS TABLE OF INTEGER;
    rows_lens stat_col_type;
    Using MULTISET UNION operations, I get the final rows_lens as a set of values, e.g. {1,2,3,1,1,2,2,2,3,3,3,3,3,3,3,1,1,2,1,1,4,5,6,2,2,2,3,3}.
    What I need is to count the occurences of the unique values :
    value of 1 : 7 occurences
    value of 2 : 8 occurences
    value of 3 : 10 occurences
    value of 4 : 1 occurence
    value of 5 : 1 occurence
    value of 6 : 1 occurence
    Ideally, the result should be a separate collection where the values become indexes (there are numbers, natural, unique and countable) and the number of occurences - their values :
    output_col( value ) := number-of-occurences ;
    Of course, I could just go through all the elements incrementing the counters in the output collection, but surely there must be a most efficient way - probably using POWERMULTISET or some kind of SUBSETS. As haven't been using /developing in/ Oracle for a longer period... it's better to ask :)
    Thanks in advance for your suggestions.
    Regards,
    Bart
    Edited by: user3698166 on 2010-01-16 13:44

    What I need is to count the occurences of the unique values :
    Following is defined:Why don't you define in PLSQL and not in SQL?
    It would be so much easier:
    SQL> create type stat_col_type as table of integer
    Type created.
    SQL> select column_value, count (column_value) cnt
      from table (
              stat_col_type (1,
                              2,
                              3,
                              1,
                              1,
                              2,
                              2,
                              2,
                              3,
                              3,
                              3,
                              3,
                              3,
                              3,
                              3,
                              1,
                              1,
                              2,
                              1,
                              1,
                              4,
                              5,
                              6,
                              2,
                              2,
                              2,
                              3,
                              3
    group by column_value
    order by column_value
    COLUMN_VALUE        CNT
               1          7
               2          8
               3         10
               4          1
               5          1
               6          1
    6 rows selected.
    SQL> drop type stat_col_type
    Type dropped.

  • Count bitpattern occurences in textstrings

    Hi
    I was wondering if there are any way to count the occurences of a bitpattern within a textstring using Java. If ANYONE can help me out with this one I would be eternal grateful.
    For instance. If I have the text in a string named str1, I want to check this for every occurence of 11's or perhaps 00's.

    I suppose so. You can always write "11+" or "00+" etc, to represent two or more contiguous occurrences of ones or zeros respectively.
    If you have your binary data in the form of a String variable you can operate on it as if it were a String. So regex works, but of course you can also write your own code searching for the pattern. It's more of an algorithm problem than a Java one.
    I think that from the java aspect you only need to look at the documentation of class String:http://java.sun.com/javase/6/docs/api/java/lang/String.html, so you can manipulate it according to your needs or see what you can do with the regular expressions.

  • Counting particular character in a string

    Hi all
    how can we count the number of characters in a perticular string in simple SQL statement
    example
    how many commas are there in this string 'a,b,s,d,e,f,g,h'

    select length('a,b,c,d,e')-length(replace('a,b,c,d,e',','))
    from dual

  • How to count the number of occurences of a character

    hi
    wat command is used to count the number of occurences of a charcter in a line?
    i have to count the number of '.' in a line

    FIND
    Searches for patterns.
    Syntax
    FIND <p> IN [SECTION OFFSET <off> LENGTH <len> OF] <text>
                [IGNORING CASE|RESPECTING CASE]
                [IN BYTE MODE|IN CHARACTER MODE]
                [MATCH OFFSET <o>] [MATCH LENGTH <l>].
    The system searches the field <text> for the pattern <p>. The SECTION OFFSET <off> LENGTH <len> OF addition tells the system to search only from the <off> position in the length <len>. IGNORING CASE or RESPECTING CASE (default) specifies whether the search is to be case-sensitive. In Unicode programs, you must specify whether the statement is a character or byte operation, using the IN BYTE MODE or IN CHARACTER MODE (default) additions. The MATCH OFFSET and MATCH LENGTH additions set the offset of the first occurrence and length of the search string in the fields <p> and <l>.

  • To count the number of occurences of a character

    Hi All,
    Is there a particular function to count the number of occurences of a particular character in a string...?
    Let me know if u know the same.
    Thanx and regards
    Akshat

    A method with a test driver:
    public class a {
         public static void main(String args[]) {
              System.out.println(howmany(args[0],args[1].charAt(0)));
         public static int howmany(String what,char c) {
              String regexp = "" + c;
              String s=null;
              try     {
                   s=what.replaceAll(regexp,"");
              catch (Exception e) {          
                    regexp = "\\" + c;
                   s=what.replaceAll(regexp,"");
              return what.length() - s.length();
    }

  • 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();
    }

  • Count the number of occurence in table with toplink

    Hi !
    there is no way to build a query with expressionbuilder or .... to count the number of occurences in my table ?
    i don't want use query " select count(*) from table "
    thanks

    Not sure of the question. Are you looking to get the sql "select count(*) from table" from using the TopLink expression framework or are you getting that SQL already and want something else?
    If you are looking just to get the count from a table/class, you can use a ReportQuery:
    ReportQuery rquery = new ReportQuery(ClassToQueryOn.class);
    rquery.addCount(); //equivalent to count(*);
    session.executeQuery(rquery);
    You can use a report query to return data instead of objects, and use selection criteria just like a normal read query.
    Best Regards,
    Chris

Maybe you are looking for