Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to count an replace substring string in 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);
Comment

how to count an replace string in java

public static void main(String[] args) throws Exception {
        String word = "abcdefg";
        System.out.println(word.length());
        System.out.println(word.replace("a", "").length());
        int a_counter = word.length() - word.replace("a", "").length();
        System.out.println(a_counter);
}
Comment

PREVIOUS NEXT
Code Example
Java :: android kotlin center text 
Java :: for each java 
Java :: java suppress warnings rawtypes 
Java :: java get ip address 
Java :: java 8 filter first 
Java :: loop list java 
Java :: add seconds to today date and time java 
Java :: how to iterate over JSONObject 
Java :: object to double java 
Java :: select word from list 
Java :: java stream sum integers 
Java :: sum of 1D array(JAVA) 
Java :: remove extra spaces java 
Java :: java_home should point to a jdk not a jre 
Java :: binary to integer in java 
Java :: Error: Could not find or load main class -Djava.library.path=.home.hdoop.hadoop-3.3.0.lib.native 
Java :: java uuid 
Java :: set jframe fullscreen 
Java :: android localdatetime 
Java :: jframe actionlistener 
Java :: qr code generator in java 
Java :: get last index of array java 
Java :: float maximum value java 
Java :: java selenium implicitly wait 
Java :: initialize class java 
Java :: How to create a popup window (PopupWindow) in Android 
Java :: java print 2d char array 
Java :: android list[index] 
Java :: caused by: java.lang.noclassdeffounderror: org/springframework/boot/configurationprocessor/json/jsonexception 
Java :: split string regex java 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =