Solved

no of occurence of ","

Posted on 2001-06-09
6
252 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fibonacci ten numbers 4 54
eclipse buid path vs tomcat lib path 10 37
throw exception 21 66
web project error add remove 1 49
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

679 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