Solved

no of occurence of ","

Posted on 2001-06-09
6
250 Views
Last Modified: 2012-05-04
How do i find the number of occurence of "," or ";"  within a string???
0
Comment
Question by:TLTEO
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6172817
int count(String s, char ch)
{
  int count = 0;
  int start = 0;
  while (-1!=(start=s.indexOf(ch, start)+1))
  {
     count++;
  }
}
0
 

Accepted Solution

by:
sobhan_genie earned 5 total points
ID: 6173000
Hey TLTEO,

    I guess this can be tried.

    StringTokenizer strtok = new StringTokenizer(str,";");
                               //str is your string with ;
    strtok.countTokens(); //This will return the no of tokens;

     you can give more than one delimiter in stringtokenizer constructor in that case probably you cant get an individual count of tokens.


bye,
Sobhan
0
 
LVL 92

Expert Comment

by:objects
ID: 6174779
Tee hee :)
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 1

Expert Comment

by:prasad2000
ID: 6176823
using stringtokenizer gives one count more

try this
static int getCount(String s, String s1)
{
 int count = 0;
 int start = 0;
 while (s.indexOf(s1 ,start)!=-1)
 {
     count++;
     s= s.substring(s.indexOf(ch)+s1.length());
 }
 return count;
}
0
 

Author Comment

by:TLTEO
ID: 6178278
Oh great.  You are absolutely right, there is one count more.  Maybe I just minus 1 fro the result.
But for cleaner look will use this
0
 
LVL 92

Expert Comment

by:objects
ID: 6179405
tee hee again :-)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question