Ad Home

Theme images by kelvinjay. Powered by Blogger.

Travel

Header Ads

Java

Selenium

UFT

Framework

General QA Concept

BDD

» » How to Count Occurance of Character in Java Using indexOf Method

In my last couple of posts..
I have shown how to do searching with string which is divided into words.

In this post I will show how to search inside a word.
just like the stack of the bangles. i want to find out the number of red color bangles present.

let me give a practical example.
I have a string say-'abcdabcdabcdabcda abcdabcdabcdabcda '

and I want to find out the occurrence of 'a' in the string

In java , there are ways to perform this activity.

1. read the string character by character.
2. check each character if it is equal to "a" then increment the counter.

Let me write the main method--


public class Search_String12 {


public static void main(String[] args)
{
String s="abcdabcdabcdabcda abcdabcdabcdabcda";
char a='a';
Search_String12 sc=new Search_String12();
System.out.println("Counter value"+sc.searchwithArray(s, a));
}


In this code I am calling sc.searchwithArray(s, a) this function Here I am passing the string and the search character...

Let me write the code for that function--

public int searchwithArray(String str,char srch){
String s=str;
char srchchar=srch;
int counter=0;
for(int i=0;i<s.length();i )
{
if (s.charAt(i)==srchchar)
{
counter ++;
}

}
return counter;
}


Now there is some advanced way--
In java there is some method called "indexOf"  and there are 4 types of it.



public int indexOf(int ch )
//It gives the first occurrence of a character
or

public int indexOf(int ch, int fromIndex)
//It gives the first occurrence of a character starting from the index specified
or

int indexOf(String str)
//it gives the first occurrence of the part of the string kind of sub string operation
or

int indexOf(String str, int fromIndex)
//It gives the first occurrence of a sub string starting from the index specified

I am going to work with this indexOf method in an advanced way where i don't need to check every character one by one.

Let us see how---
I am creating the function..


public int getCount(String str,String srch) {
String s=str;
String srchchar=srch;
//initializing the variables
int val=0;
int counter=0;
int i=-1;

//this for loop starts with -1..this is required to check if the character present in the 0th position- //i.e.start position of the string.

for(i=-1;;)
{
val=s.indexOf(srchchar, i);
//this if is to continue search if the character found at first position of the string.
if(val==0)
{
i=val+1;
counter++;
}

// the indexOf function returns the position if the search character found in the string but returns -1 if //not found. this condition checks if no character in the string

else if(val!=-1) {
i=val+1;
counter++;
}

//This is the termination condition of the loop. there might be two conditions on which we need to //break..
//1. if all positions are checked...for that i have used i>s.length()
//2. if no character found when val==-1

if(i>s.length()||val==-1) {
break;
}
}

return counter;
}

Let me create the main function now--
public static void main(String[] args)
{
String s="abcdabcdabcdabcda abcdabcdabcdabcda";
String a="a";
Search_String12 sc=new Search_String12();
System.out.println("Counter value"+sc.getCount(s, a));

}

I have used the flavor where indexOf function takes String and index as parameters.
You can however use the character and index method.

The entire code is written below:


public class Search_String12 {
 

    public static void main(String[] args)
    {
        String s="abcdabcdabcdabcda abcdabcdabcdabcda";
        //String a="a";
        char a='a';
        Search_String12 sc=new Search_String12();
      //  System.out.println("Counter value"+sc.getCount(s, a));
        System.out.println("Counter value"+sc.searchwithArray(s, a));
    }

public int getCount(String str,String srch) {
    String s=str;
    String srchchar=srch;
    int val=0;
    int counter=0;
    int i=-1;
   for(i=-1;;)
    {
       val=s.indexOf(srchchar, i);
        if(val==0)
      {
           i=val+1;
           counter++;
      }
        else if(val!=-1) {
          i=val+1;
          counter++;
      }
        if(i>s.length()||val==-1) {
            break;
        }
    }
   
    return counter;
}
public int searchwithArray(String str,char srch){
    String s=str;
    char srchchar=srch;
    int counter=0;
        for(int i=0;i<s.length();i++)
    {
    if (s.charAt(i)==srchchar)
    {
        counter++;
    }

}
    return counter;
}
}
 
Just change the function call and parameter types whenever you are calling these functions.

«
Next
Newer Post
»
Previous
Older Post

1 comments: