Solved

no of occurence of ","

Posted on 2001-06-09
6
248 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 3 50
solarwind tftp server 2 45
eclipse formatting 6 73
github account with ecipse 1 42
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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 …

867 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

18 Experts available now in Live!

Get 1:1 Help Now