Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Sunday, November 15, 2015

Count the no of occurance of a string into string

This is solution for how to count the number of occurrence of string into string.

package pratik_substring;
import java.io.*;
import java.util.*;
import java.lang.*;

public class Substringdemo
{
   
   
    public static int countMatches(String str, String sub) {
        if (str.equals(" ") || sub.equals(" ")) {
            return 0;
        }
        int count = 0;
        int idx = 0;
        while ((idx = str.indexOf(sub, idx)) != -1) {
            count++;
            idx += sub.length();
        }
        return count;
    }
   
    public static void main(String args[])throws IOException
    {
        String s;
        String a;
        InputStreamReader is=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(is);
       
        System.out.println("Enter String");
        s=br.readLine();
       
        System.out.println("Enter sub string");
        a=br.readLine();
       
        int a1=countMatches(s,a);
        System.out.println("No of Occurance of " + a + " into " + s + " is :- " + a1);
       
    }
}

Some more String Functions which you may never heard about:-

1) charAt
    Syntax :- charAt(string,integer)
    Description :- Returns character at index in the string.

2) contains
    Syntax :- contains(string,substring)  returns boolean
    Description :-Returns whether string contains substring as a substring. This is equivalent to using Find and then detecting that it does not result in the failure condition listed in the third column of the Find section. However, some languages have a simpler way of expressing this test.

3) rfind
    Syntax :- rfind(string,substring) returns integer
    Description :- Returns the position of the start of the last occurrence of substring in string. If the      substring is not found most of these routines return an invalid index value – -1 where indexes are 0-based, 0 where they are 1-based – or some value to be interpreted as Boolean FALSE.

4) join
    Syntax :- join(separator, list_of_strings) returns a list of strings joined with a separator
    Description :- Joins the list of strings into a new string, with the separator string between each of the substrings. Opposite of split.
                   Related to sprintf

5) partition
    Syntax :- <string>.partition(separator) returns the sub-string before the separator; the separator; then the sub-string after the separator.
    Description :- Splits the given string by the separator and returns the three substrings that together make the original.

6) split
    Syntax :- <string>.split(separator[, limit]) splits a string on separator, optionally only up to a limited number of substrings
    Description :- Splits the given string by occurrences of the separator (itself a string) and returns a list (or array) of the substrings. If limit is given, after limit - 1 separators have been read, the rest of the string is made into the last substring, regardless of whether it has any separators in it. The Scheme and Erlang implementations are similar but differ in several ways. JavaScript differs also in that it cuts, it does not put the rest of the string into the last element. See the example here. The Cobra implementation will default to whitespace. Opposite of join.


Protected by Copyscape