how to count an replace substring string in java
Java
String str = "helloslkhellodjladfjhello";
String findStr = "hello";
int lastIndex = 0;
int count = 0;
while(lastIndex != -1){
lastIndex = str.indexOf(findStr,lastIndex);
if(lastIndex != -1){
count ++;
lastIndex += findStr.length();
}
}
System.out.println(count); Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
String sub = scanner.nextLine();
int count = 0;
int fromIndex =0;
while( (fromIndex = line.indexOf(sub,fromIndex) ) != -1) {
count++;
fromIndex++;
}
System.out.println(count);
Also in Java:
- Title
- HTTP FAILED: java.io.InterruptedIOException: interrupted
- Category
- Java
- Title
- esponente in java
- Category
- Java
- Title
- java get location of jar file
- Category
- Java
- Title
- properties java 8 maven in pom xml
- Category
- Java
- Title
- java random number between 100 and 999
- Category
- Java
- Title
- java not instanceof
- Category
- Java
- Title
- do statement java
- Category
- Java
- Title
- java loop
- Category
- Java
- Title
- sort elements with sortedset
- Category
- Java
- Title
- 2d array length in java
- Category
- Java
- Title
- calculating the percentile in java
- Category
- Java
- Title
- how to read to into a file in java
- Category
- Java
- Title
- java append to array
- Category
- Java
- Title
- java loop through array
- Category
- Java
- Title
- transformer un string en Biginteger java
- Category
- Java
- Title
- can we overload a static method in java
- Category
- Java
- Title
- how to change double to int in java
- Category
- Java
- Title
- java creare costante
- Category
- Java
- Title
- java method
- Category
- Java
- Title
- countdown timer with seekbar
- Category
- Java
- Title
- not equal to java
- Category
- Java
- Title
- how to get the dimensions of a 2d array in java
- Category
- Java
- Title
- run spring boot application command line
- Category
- Java
- Title
- iterator for array java
- Category
- Java
- Title
- java jaxb unmarshall xml to map
- Category
- Java
- Title
- selection sort in java
- Category
- Java
- Title
- array null pointer java
- Category
- Java
- Title
- how to count the number of occurrences of an element in a arraylist in java
- Category
- Java
- Title
- read csv in java in spring
- Category
- Java
- Title
- compile java to jar in mac
- Category
- Java
- Title
- how to check internet is working or not in java
- Category
- Java
- Title
- two dimensional array in java example program
- Category
- Java
- Title
- print a letter in java
- Category
- Java
- Title
- ceil function in java
- Category
- Java
- Title
- how to copy list item to another list in java
- Category
- Java
- Title
- jstl core tags
- Category
- Java
- Title
- system.out.println shortcut
- Category
- Java
- Title
- count the number of words in a string java
- Category
- Java
- Title
- how to compare string in java
- Category
- Java
- Title
- what is a method example in java
- Category
- Java
- Title
- find duplicate elements in array in java
- Category
- Java
- Title
- how to create a linked list in java
- Category
- Java
- Title
- room insert and return id
- Category
- Java
- Title
- how to add two map values in java
- Category
- Java
- Title
- java stack push
- Category
- Java
- Title
- number to string java
- Category
- Java
- Title
- how to check type of primitive value in java
- Category
- Java
- Title
- how to create java jframe in eclipse
- Category
- Java
- Title
- android create snackbar
- Category
- Java
- Title
- string to char in java
- Category
- Java
- Title
- imageview.setbackground
- Category
- Java
- Title
- how to print ascii value in java
- Category
- Java
- Title
- java 8 retrieve all list from object into single list and ignore duplicates
- Category
- Java
- Title
- how to make a dictionary in java
- Category
- Java
- Title
- java how to convert string to int
- Category
- Java
- Title
- java read each lines in file
- Category
- Java
- Title
- java permutation
- Category
- Java
- Title
- java mockito any string import
- Category
- Java
- Title
- find frequency of each word in a string in java
- Category
- Java
- Title
- java how to make a string lowercase
- Category
- Java
- Title
- how to collect objective in java
- Category
- Java
- Title
- print values of bst java
- Category
- Java
- Title
- java run project from command line
- Category
- Java
- Title
- how to play a clip of audio in java when needed
- Category
- Java
- Title
- spring framework iterate
- Category
- Java
- Title
- java get icon for file type
- Category
- Java
- Title
- reverse linked list in java to get both head and tail
- Category
- Java
- Title
- print a list java
- Category
- Java
- Title
- how to iterate list in java selenium
- Category
- Java
- Title
- inheritance setter and getter in java
- Category
- Java
- Title
- while loops java
- Category
- Java
- Title
- make a commet in java
- Category
- Java
- Title
- Jlabel icon
- Category
- Java
- Title
- java thread
- Category
- Java
- Title
- java map geeksforgeeks
- Category
- Java
- Title
- using class in java
- Category
- Java
- Title
- lopp array java
- Category
- Java
- Title
- method resolve file in java
- Category
- Java
- Title
- select photo from camera android
- Category
- Java
- Title
- java for
- Category
- Java
- Title
- place.getlatlng() returning null
- Category
- Java
- Title
- run java program maven
- Category
- Java
- Title
- java timestamp
- Category
- Java
- Title
- filter and map multiple fields from java stream
- Category
- Java
- Title
- how to add a number to an array in java
- Category
- Java
- Title
- iterate map in java
- Category
- Java
- Title
- java intercept ctrl c
- Category
- Java
- Title
- priority queue java reverse order
- Category
- Java
- Title
- find maven version
- Category
- Java
- Title
- java set to list
- Category
- Java
- Title
- generate objects with for loop java
- Category
- Java
- Title
- java check if int
- Category
- Java
- Title
- arraylist in java
- Category
- Java
- Title
- generate all prime number less than n java (fastest method)
- Category
- Java
- Title
- java generic calling clone method
- Category
- Java
- Title
- java hashtable
- Category
- Java
- Title
- LocalRegistry java rebind() java8
- Category
- Java
- Title
- hello world java
- Category
- Java
- Title
- throwing exceptions java
- Category
- Java
- Title
- entre clavier java
- Category
- Java