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





Post a Comment

Copyright © Rough Record. Designed by OddThemes