Skip to Content
author's profile photo Former Member
Former Member

Code to count number of string appearances and return a nunber of counts

Hi All

I am trying to count the number of appearance of my the string when so my UDF code is:

public String StringCounter(String BM_CH_ESS,Container container)


String searchFor = "BM_CH_ESS";

String value = null;

int searchLength = searchFor.length();

int Count = 0;

int index = 0;

for( int i = 0; i < value.length(); i++ )


index = value.indexOf(searchFor, i);

if ( index != -1 )



i += index + searchLength;



return Count;


When ever press F7 I get the error below:

Basical I want to return a number of counts.

Source code has syntax error: C:/usr/sap/###/DVEBMGS00/j2ee/cluster/server0/./temp/classpath_resolver/Map6dc873d0e26211df88c6000c2973cb67/source/com/sap/xi/tf/ incompatible types found : int required: java.lang.String return Count;

Can u please give some suggestions



Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Best Answer
    Posted on Oct 28, 2010 at 08:02 AM

    > public String StringCounter(String BM_CH_ESS,Container container)

    > int Count = 0;

    > return Count;

    You have to return a String, not an int.

    Test your Java code with an external tool first:


    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Oct 28, 2010 at 02:13 PM


    Why you are using for loop here? What's your actual requirement?

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi ALL

      Thanks for ur assistance, I managed to find the solution I went to my NetBeans aditor to write it:

      I wanted to write a function like this to count the string appearance"

      This is my below code:

      public class Main {


      • @param args the command line arguments


      public static void main(String[] args)


      String inputString = "C:C:C";

      String searchFor = "C";

      int leng = searchFor.length();

      int count = 0;

      if(leng > 0)


      int Index = inputString.indexOf(searchFor);

      while(Index != -1)



      Index = inputString.indexOf(searchFor, Index + leng );



      System.out.println("Count = " + count);



      Thanks inadvance


Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.