Solved

no of occurence of ","

Posted on 2001-06-09
6
253 Views
Last Modified: 2012-05-04
How do i find the number of occurence of "," or ";"  within a string???
0
Comment
Question by:TLTEO
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Facing this issue for maven proxy setting 2 46
Selenium docs api java index 3 98
program arguments vs VM arguments 4 60
import as existing maven project 3 78
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…
Suggested Courses

710 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