public class Split2CharFinal
{
public static void main(String[] args)
{
// You can put main method code in Process Request or Process Form Request
String str = "SplitChar2";
// Split String with fixed Number of Char eg. 2
Object[] strOut;
// Call Method to get Splitted String in Array
strOut = splitInParts(str, 2);
String str_Full_Split = "";
// Read Array
for (int i=0; i<strOut.length; i++) {
System.out.println(" Out "+i+" "+strOut[i]);
str_Full_Split = str_Full_Split +"/"+ strOut[i];
}
System.out.println(" Full Split "+ str_Full_Split);
}
public static String[] splitInParts(String s, int partLength) {
int len = s.length();
// Number of parts
int nparts = (len + partLength - 1) / partLength;
String parts[] = new String[nparts];
// Break into parts
int offset= 0;
int i = 0;
while (i < nparts) {
parts[i] = s.substring(offset, Math.min(offset + partLength, len));
offset += partLength;
i++;
}
return parts;
}
}
Saturday, August 18, 2012
Split Characters with Specific Length
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment