How to split a string at every Nth occurrence of a character in Java -


i'm trying split string @ every nth occurence, missing last values.here expected.

input : string str = "234-236-456-567-678-675-453-564";

output :

234-236-456   567-678-675   453-564  

here n=3, str should split @ every 3rd occurence of -.

maybe 1 of worst way without using function available in java , exercise :

public static void main(string[] args){         string s = "234-236-456-567-678-675-453-564";         int nth =0;         int cont =0;         int i=0;         for(;i<s.length();i++){             if(s.charat(i)=='-')                 nth++;             if(nth == 3 || i==s.length()-1){                 if(i==s.length()-1) //with if preveent cut last number                 system.out.println(s.substring(cont,i+1));                 else                     system.out.println(s.substring(cont,i));                 nth=0;                 cont =i+1;               }         }     } 

Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -