Solved

no of occurence of ","

Posted on 2001-06-09
6
247 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
Comment Utility
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
Comment Utility
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
Comment Utility
Tee hee :)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Expert Comment

by:prasad2000
Comment Utility
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
Comment Utility
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
Comment Utility
tee hee again :-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now