19. Program to divide a string in 'N' equal parts.
class Main{
static String[] strBreak(String str, int n){
String st[] = new String[n];
if(n < 1 || (str.length()%n) != 0){
System.out.println("Partition Unavailable");
return null;
}
for(int i = 0, k = 0; i < (str.length() - (str.length()/n) +1); k++){
st[k] = "";
for(int j = 0; j < (str.length()/n); j++){
st[k] += str.charAt(i);
i++;
}
}
return st;
}
public static void main(String[] args){
String[] st = strBreak("HelloWorld12", 2);
if(st != null){
System.out.println("Parts of String are : ");
for(int i = 0; i < st.length; i++){
System.out.println("Part No " + (i+1) + " : " + st[i]);
}
}
}
}
OUTPUT
Parts of String are :
Part No 1 : HelloW
Part No 2 : orld12