09 April 2012

Java String Occurrence in a String


import java.util.*;
import java.io.*;
import java.lang.String;

public class StringCount{
  public static void main(String args[]){
  String search = "is";
  String main = "This is this";
  int length = search.length();
  int result = 0;
  if (length > 0) {
  int flag = main.indexOf(search);
  while (flag != -1) {
  result++;
  flag = main.indexOf(search, flag+length);
  }
  }
  System.out.println(result);
  }
}

/*
output
string count 3
*/

IndexOf()


public class stringDemo{
  public static void main(String[] argsthrows Exception{
  String test = "angel";
  int index = test.indexOf('e')
  System.out.println(index);
  }
}

/*
output
4
*/


replaceFirst()


public class ReplaceFirst {
  public static void main(String[] args) {
  String string = "Hello World";
  String stringreplace = "Hi";
  String result = string.replaceFirst("Hello", stringreplace);
  System.out.println(result);
  }
}



/*


ouptut
Hi World





1 comments:

Naviya Nair said...

I have read your blog its very attractive and impressive. I like it your blog.

Java Online Training Java EE Online Training Java EE Online Training Java 8 online training Core Java 8 online training

Java Online Training from India Java Online Training from India Core Java Training Online Core Java Training Online Java Training InstitutesJava Training Institutes

Post a Comment

Related Posts with Thumbnails

Popular Posts

 
;