splitting string in java into fixed length chunks - java

I have seven strings in a program named string1 through string7.
The size of each of the string will be 30 characters.
I will get a input string of unknown length.
I have to split this input string in 30 char strings and then set first substring into string1, 2nd in string2 and so on until possible. If input string is greater then 210 characters then remaining string at the end will be ignored.
How to handle the case when the input string is of size smaller then 210 char.
For e.g. 145 in which case string1 through string4 will be full and string5 will be made of remaining 15 char.
How to handle this nicely ?
I can do it reading char by char and putting first 30 char and string1, next in string2, etc until all char are consumed.
But is there a better way to do this ?

If you can use third-party libraries, with Guava this is just
Iterable<String> chunks = Splitter.fixedLength(30).split(string);
This can be converted to a List<String> with e.g. Lists.newArrayList.
(Disclosure: I contribute to Guava.)

private static Collection<String> splitStringBySize(String str, int size) {
ArrayList<String> split = new ArrayList<>();
for (int i = 0; i <= str.length() / size; i++) {
split.add(str.substring(i * size, Math.min((i + 1) * size, str.length())));
}
return split;
}

Since your Strings are not in an array or List you need to assign them explicitely.
Matcher m = Pattern.compile(".{1,30}").matcher(s);
String s1 = m.find() ? s.substring(m.start(), m.end()) : "";
String s2 = m.find() ? s.substring(m.start(), m.end()) : "";
String s3 = m.find() ? s.substring(m.start(), m.end()) : "";
String s4 = m.find() ? s.substring(m.start(), m.end()) : "";
String s5 = m.find() ? s.substring(m.start(), m.end()) : "";
String s6 = m.find() ? s.substring(m.start(), m.end()) : "";
String s7 = m.find() ? s.substring(m.start(), m.end()) : "";

How about using a char array for splitting the string, create a general-use method receiving the chunk size and maximum size to consider, and returning a String array?
public class SplitStringIntoFixedSizeChunks {
public static String[] Split(String text, int chunkSize, int maxLength) {
char[] data = text.toCharArray();
int len = Math.min(data.length,maxLength);
String[] result = new String[(len+chunkSize-1)/chunkSize];
int linha = 0;
for (int i=0; i < len; i+=chunkSize) {
result[linha] = new String(data, i, Math.min(chunkSize,len-i));
linha++;
}
return result;
}
public static void main(String[] args) {
String x = "flskdafsld~fdsakçkçfsda sfdaldsak~çfdskkfadsçlkçfldskçlflçfdskçldksçlkfdslçakafdslçdsklçfdskçlafdskçkdfsçlkfds~çlkfasdçlçfdls~kçlf~dksçlsakdçlkfç";
System.out.println("x length: "+x.length());
String[] lines = Split(x, 30, 210);
for (int i=0; i < lines.length; i++) {
System.out.println("lines["+i+"]: (len: "+lines[i].length()+") : "+lines[i]);
}
}
}
This example results:
x length: 145
lines[0]: (len: 30) : flskdafsld~fdsakçkçfsda sfdald
lines[1]: (len: 30) : sak~çfdskkfadsçlkçfldskçlflçfd
lines[2]: (len: 30) : skçldksçlkfdslçakafdslçdsklçfd
lines[3]: (len: 30) : skçlafdskçkdfsçlkfds~çlkfasdçl
lines[4]: (len: 25) : çfdls~kçlf~dksçlsakdçlkfç

This is something which should work:
String str = "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
if (str.length() > 210)
{
str = str.substring(0, 209);
}
String newStr = str.replaceAll("(.{30})", "$1|");
System.out.println(newStr);
String[] newStrings = newStr.split("\\|");
What it does is that it takes the given string and at every 30 characters, it throws in a seperator. In this case, I am assuming that you have an idea of what will the user enter and what not so that you can throw in a seperator (or group of seperators) which the user will not enter. Once I do that, I split the string using the seperator I have just added.

This might help you.
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String st = br.readLine();
int len = st.length();
String[] str = new String[7];
len=len/30;
int i=0;
for(; i<7 && i<len; i++ ){
str[i] = st.substring(30*i, 30*(i+1));
System.out.println(str[i]);
}
if(i!=7){
str[i] = st.substring(30*i, st.length());
System.out.println(str[i]);
}
}

I ran into an issue with a specific usage of this technique. A user was copy/pasting M$ Word content into an HTML field that eventually was picked up by this technique to be split into multiple database fields.
The technique broke against M$ Word's use of carriage returns and other ASCII characters. The REGEX would split off each carriage return instead of a specified number of characters. To correct the issue, I modified Michael Besteck's code to the following:
Matcher m = Pattern.compile(".{1,30}", Pattern.DOTALL).matcher(s);
String s1 = m.find() ? s.substring(m.start(), m.end()) : "";
String s2 = m.find() ? s.substring(m.start(), m.end()) : "";
String s3 = m.find() ? s.substring(m.start(), m.end()) : "";
String s4 = m.find() ? s.substring(m.start(), m.end()) : "";
String s5 = m.find() ? s.substring(m.start(), m.end()) : "";
String s6 = m.find() ? s.substring(m.start(), m.end()) : "";
String s7 = m.find() ? s.substring(m.start(), m.end()) : "";
This accounts for the ASCII characters correctly.

This is what I did. Seems to work. Please comment if I am wrong anywhere:
package com.mypackage;
import java.util.ArrayList;
import java.util.List;
public class TestClass {
public static List<String> splitEqually(final String text, final int size) {
List<String> ret = new ArrayList<String>((text.length() + size - 1) / size);
for (int start = 0; start < text.length(); start += size) {
if (start + size > 0) {
String temp = text.substring(start, Math.min(text.length(), start + size));
int length = temp.length();
for (int i = 0; i < (size - length); i++) {
temp = temp + " ";
}
ret.add(temp);
} else {
ret.add(text.substring(start, Math.min(text.length(), start + size)));
}
}
return ret;
}
public static void main(final String args[]) {
String input = "hello wo";
String str1, str2, str3, str4, str5;
List<String> result = TestClass.splitEqually(input, 3);
try {
str1 = result.get(0);
System.out.println("1: " + result.get(0));
str2 = result.get(1);
System.out.println("2: " + result.get(1));
str3 = result.get(2);
System.out.println("3: " + result.get(2));
str4 = result.get(3);
System.out.println("4: " + result.get(3));
str5 = result.get(4);
System.out.println("5: " + result.get(4));
} catch (IndexOutOfBoundsException e) {
}
}
}

import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
// your input
String input = "";
// strings 1-7 as array list
ArrayList<String> results = new ArrayList<String>();
int length;
if(input.length() > 210) {
length = 210;
} else {
length = input.length();
}
for(int i = 0; i <= length; i += 30) {
if((length - (i + 30)) > 0) {
results.add(input.substring(i, i + 30));
} else {
results.add(input.substring(i, length));
}
}
for(int a = 0; a < results.size(); a++) {
System.out.println("size: " + results.get(a).length() + " content: " + results.get(a));
}
}
}

private Collection<String> splitStringBySize(String strPrm, int sizePrm) {
ArrayList<String> splitLcl = new ArrayList<>();
int lLcl=strPrm.length();
int quantityLcl = lLcl / sizePrm;
for (int i = 0; i < quantityLcl; i++) {
splitLcl.add(strPrm.substring(i * sizePrm,(i+1) * sizePrm ));
}
int tailLcl = lLcl % sizePrm;
if(tailLcl>0){
splitLcl.add(strPrm.substring(lLcl-tailLcl,lLcl));
}
return splitLcl;
}

Using Streams, this one should do it in an easy way:
private static Stream<String> splitToFixedSize(String input, int maxSize) {
var noOfChunks = (int) Math.ceil((float) input.length() / maxSize);
return IntStream.range(0, noOfChunks).mapToObj(i -> {
var start = i * maxSize;
var end = Math.min((i + 1) * maxSize, input.length());
return input.substring(start, end);
});
}

Related

Replacing the number with the number of spaces

I have a String:
String example = "AA5DD2EE3MM";
I want to replace the number with the number of spaces. Example:
String example = "AA DD EE MM"
If the String would be
String anotherExample = "a3ee"
It should turn into:
String anotherExample = "a ee"
I want to do it for any string. Not only for the examples above.
Split your input at digit and non digit chars as a stream, map digits to the corsponding number of spaces using String.repeat, collect to string using Collectors.joining():
String input = "AA5DD2EE3MM";
String regex = "(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)";
String result = Pattern.compile(regex)
.splitAsStream(input)
.map(s -> s.matches("\\d+") ? " ".repeat(Integer.parseInt(s)) : s)
.collect(Collectors.joining());
You could also use this approach, which is simpler but also far less elegant:
String example = "a4aa";
String newString = "";
for (int i = 0; i < example.length(); i++) {
if (Character.isDigit(example.charAt(i))) {
for (int a = 0; a < Character.getNumericValue(example.charAt(i)); a++) {
newString += " ";
}
} else {
newString += example.charAt(i);
}
}
System.out.println(newString);
Using a pattern matcher approach:
String input = "AA5DD2EE3MM";
Pattern pattern = Pattern.compile("\\d+");
Matcher m = pattern.matcher(input);
StringBuffer buffer = new StringBuffer();
while (m.find()) {
m.appendReplacement(buffer,new String(new char[Integer.valueOf(m.group())]).replace("\0", " "));
}
m.appendTail(buffer);
System.out.println(buffer.toString()); // AA DD EE MM
The idea here is to iterate the string, pausing at each digit match. We replace each digit with space replicated the same number of times as the digit.
public static String replaceDigitsWithSpaces(String input) {
String result = "";
int len = input.length(), i =0;
while( i < len) {
if(Character.isLetter(input.charAt(i))) {
result += input.charAt(i);
}else if(Character.isDigit(input.charAt(i))) {
//generate number upto characters
int k = 0, j = i;
String temp = "";
while(j < len) {
if(Character.isDigit(input.charAt(j))) {
temp += input.charAt(j);
j++;
}else {
break;
}
}
k = Integer.parseInt(temp);
while(k != 0) {
result+= " ";
k--;
}
i = j;
continue;
}
i++;
}
return result;
}
input:: "AA23BB1C11C8"<br>
output:: AA BB C C .
StringBuilder is more efficient for concatenation:
public static String spaceIt(String s) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (Character.isDigit(c)) {
for (int j = 0; j < Character.digit(c, 10); j++) {
sb.append(' ');
}
} else {
sb.append(c);
}
}
return sb.toString();
}

how to cut a string every n characters? but it only cuts when there are spaces

I am trying to cut a long string into lines of that string, the length of the lines is decided by the function. The function will not cut the words in the middle.
I have attempted many ways to do so using substrings and such but I am not that great at string manipulation. I have found a similar issue online, but it was in JavaScript and some of the code I could not fully translate to Java (maybe because i'm inexperienced with it...)
public static List<String> descriptionFormatter(String string, int amt)
{
String[] splitted = string.split(" ");
String part = "";
List<String> finalDesc = new ArrayList<String>();
for(int i = 0 ; i < splitted.length; i++)
{
part = part + " " + splitted[i];
if(part.length() >= amt)
{
finalDesc.add(part);
part = "";
}
}
return finalDesc;
}
e.g.
I have a string "hello world apple orange grapes juice spagehtti sauce milk"
and I want to cut it every 34 characters (considering all the requirements above)
so I call
descriptionFormatter(string, 34);
wanted result a string array/list:
hello world apple orange grapes juice
spagehtti sauce milk
actual result:
hello world apple orange grapes juice
I have gotten as far as almost getting it to work, but it sometimes it skips the remaining words at the end and puts spaces before the first word. How do I make it function as I intend it to?
You could try traversing the input string with two indexes say beginIndex and endIndex and take substrings from the input string as you go:
public static List<String> descriptionFormatter(String str, int amt) {
List<String> result = new ArrayList<>();
// trim the input string to avoid empty strings at the end
str = str.trim();
int beginIndex = 0;
int endIndex = amt;
final int length = str.length();
while(endIndex < length) {
// if we landed on something other than space
// increase the end index for the substring
// until we hit a space
while(endIndex < length && str.charAt(endIndex) != ' ') {
++endIndex;
}
result.add(str.substring(beginIndex, endIndex).trim());
beginIndex = endIndex;
endIndex += amt;
}
// Add the last string if any left
if(beginIndex < length) {
result.add(str.substring(beginIndex).trim());
}
return result;
}
public static void main(String[] args) {
String str = "hello world apple orange grapes juice spagehtti sauce milk";
descriptionFormatter(str, 34).forEach(System.out::println);
}
Output:
hello world apple orange grapes juice
spagehtti sauce milk
Try doing it like this
static List<String> split(String s, int size) {
return split(s.toCharArray(), size);
}
static List<String> split(char[] s, int size) {
List<String> strs = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length; ++i) {
if (i % size == 0) {
if(s[i] == ' ') {
strs.add(sb.toString());
sb = new StringBuilder();
}else {
StringBuilder newStringBuilder = new StringBuilder();
int length = sb.length();
while (length > 0 && sb.charAt(length - 1) != ' ') {
newStringBuilder.insert(0, sb.charAt(length - 1));
sb.deleteCharAt(length - 1);
--length;
}
if(sb.length() > 0) strs.add(sb.toString());
sb = newStringBuilder;
}
}
sb.append(s[i]);
}
if (sb.length() > 0) {
strs.add(sb.toString());
}
return strs;
}
public static List<String> descriptionFormatter(String string, int amt) {
List<String> stringPieces = new ArrayList<>();
StringBuilder strOfMaxLen = new StringBuilder();
StringBuilder strExceedsMaxLen = new StringBuilder();
String[] splitted = string.split(" ");
for (int i = 0 ; i < splitted.length; i++) {
String piece = splitted[i];
int pieceLen = piece.length();
if (strOfMaxLen.length()+pieceLen < amt) {
if (strOfMaxLen.length() != 0) {
strOfMaxLen.append(" ");
}
strOfMaxLen.append(piece);
} else {
if (strExceedsMaxLen.length() != 0) {
strExceedsMaxLen.append(" ");
}
strExceedsMaxLen.append(piece);
}
}
stringPieces.add(strOfMaxLen.toString());
stringPieces.add(strExceedsMaxLen.toString());
return stringPieces;
}
and yet another :)
private static List<String> descriptionFormatter(String string, int amt){
String[] splitted = string.split(" ");
List<String> ans = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for (String s : splitted) {
if (sb.length() + s.length() > amt) {
ans.add(sb.toString()); // end the line add to list
sb.setLength(0); //clear the current string
} else {
if (sb.length()!=0)
sb.append(" "); // if it's the first don't add a space
sb.append(s); // add the word
}
}
ans.add(sb.toString());// add the last line
return ans;
}

How do i Reverse Capitalize And Separate Words By Line Using String In Java?

The input is: i love cake
The output needs to be: Cake Love I
But the actual result is: I Love Cake
What I've got:
import java.util.regex.Pattern;
public class yellow {
static String reverseWords(String str){
Pattern pattern = Pattern.compile("\\s");
String[] temp = pattern.split(str);
String result = "";
for (int i = 0; i < temp.length; i++) {
if (i == temp.length - 1)
result = temp[i] + result;
else
result = " " + temp[i] + result;
}
return result;
}
public static void main(String[] args){
String source = "i love cake";
StringBuffer res = new StringBuffer();
String[] strArr = source.split(" ");
for (String str : strArr) {
char[] stringArray = str.trim().toCharArray();
stringArray[0] = Character.toUpperCase(stringArray[0]);
str = new String(stringArray);
res.append(str).append(" ");
}
System.out.print(res.toString());
}
}
What am I doing wrong?
for (String str : strArr) {
}
This loops forward. What you want is to loop backwards, or to place elements into the string backwards. I recommend you loop backwards and print as you go:
for (int i = strArr.length - 1; i >= 0; i--) {
char[] stringArray = strArr[i].trim().toCharArray();
stringArray[0] = Character.toUpperCase(stringArray[0]);
System.out.println(new String(stringArray));
}
Or, you could use that convenient reverseWords method that you never use anywhere... though looping backwards is faster. Probably.
[EDITED]
Call this for each line with string s, then print a line break (If you have multiple sentences & expect them in their own lines).
void reverseCamel(String s){
String[] ar = s.split("\\s+");
for(int i = ar.length - 1;i>=0;i--){
ar[i][0] = Character.toUpperCase(ar[i][0]);
System.out.print(ar[i] + " ");
}
}
Here is what i did.
public class Main {
public static void main(String[] args) {
reverse("I Love Cake");
}
public static void reverse( String string){
String[] word =string.split(" "); // split by spaces
int i = word.length-1;
while (i>=0){
// System.out.print(word[i].toUpperCase()+" ");//if you want in upper case
System.out.print(word[i]+" ");
i--;
}
}
}
First of all you have to reverse the String.
String[] words = source.split("\\s");
String reversedString = "";
for(int i = words.length -1; i>=0; i--){
reversedString += words[i] + " ";
}
Then, you know that the ASCII code of 'a' character is 97, 'A' is 65. To convert from lower case to capital you substract 32. All capitals are between 65 and 92. All small letters are between 97 and 124.
You want to capitalize only letters at the beginning of a word (preceded by a space or first letter).
String capitalCase = "";
for (int i = 0; i < reversedString.length(); i++) {
char c = reversedString.charAt(i);
if (c >= 97 && c <= 124) {
if (i == 0) c -= 32;
else if ((reversedString.charAt(i - 1) + "").equals(" ")) c -= 32;
}
capitalCase += c;
}
And here you go now System.out.println(capitalCase);
Overall, you will have the following code:
import java.util.Scanner;
public class yellow {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter a string:");
String source = s.nextLine();
String[] words = source.split("\\s");
String reversedString = "";
for (int i = words.length - 1; i >= 0; i--) {
reversedString += words[i] + " ";
}
String capitalCase = "";
for (int i = 0; i < reversedString.length(); i++) {
char c = reversedString.charAt(i);
if (c >= 97 && c <= 124) {
if (i == 0) c -= 32;
else if ((reversedString.charAt(i - 1) + "").equals(" ")) c -= 32;
}
capitalCase += c;
}
System.out.println(capitalCase);
}
}
Output:
Enter a string:
i love cake
Cake Love I
Java 8 * Apache Commons Lang
public static String reverseWordsInString(String str) {
List<String> words = Pattern.compile("\\s+").splitAsStream(str)
.map(StringUtils::capitalize)
.collect(LinkedList::new, LinkedList::addFirst, (a, b) -> a.addAll(0, b));
return words.stream().collect(Collectors.joining(StringUtils.SPACE));
}
Java 8
public static String reverseWordsInString(String str) {
List<String> words = Pattern.compile("\\s+").splitAsStream(str)
.map(word -> Character.toUpperCase(word.charAt(0)) + word.substring(1).toLowerCase())
.collect(LinkedList::new, LinkedList::addFirst, (a, b) -> a.addAll(0, b));
return words.stream().collect(Collectors.joining(" "));
}

Comparing two strings by character in java

I have 2 strings :
first= "BSNLP"
second = "PBN" (or anything that user enters).
Requirement is , O/P should return me the string with only those characters in first but not in second.
Eg. in this case O/P is SL
Eg2.
first = "ASDR"
second = "MRT"
, o/p = "ASD"
For this, the coding I have developed:
String one = "pnlm";
String two ="bsnl";
String fin = "";
for(int i =0; i<one.length();i++)
{
for(int j=0;j<two.length();j++)
{
//System.out.print(" "+two.charAt(j));
if(one.charAt(i) == two.charAt(j))
{
fin+=one.charAt(i);
}
}
}
ch=removeDuplicates(fin);
System.out.print(" Ret ::"+fin);
System.out.println("\n Val ::"+ch);
CH gives me the string with equal characters, but using this logic i cant get the unequal characters.
Can anyone please help?
You can use the Set interface to add all the second array of character so you can check it there later.
sample:
String one = "ASDR";
String two ="MRT";
StringBuilder s = new StringBuilder();
Set<Character> set = new HashSet<>();
for(char c : two.toCharArray())
set.add(c); //add all second string character to set
for(char c : one.toCharArray())
{
if(!set.contains(c)) //check if the character is not one of the character of second string
s.append(c); //append the current character to the pool
}
System.out.println(s);
result:
ASD
I have simple exchange your logic, see:
String one = "pnlm";
String two = "bsnl";
String fin = "";
int cnt;
for (int i = 0; i < one.length(); i++) {
cnt = 0; // zero for no character equal
for (int j = 0; j < two.length(); j++) {
// System.out.print(" "+two.charAt(j));
if (one.charAt(i) == two.charAt(j)) {
cnt = 1; // ont for character equal
}
}
if (cnt == 0) {
fin += one.charAt(i);
}
}
System.out.print(" Ret ::" + fin);
o/p: Ret ::pm.
public static void main(String[] args)
{
String one = "ASDR";
String two ="MRT";
String fin = unique(one, two);
System.out.println(fin);
}
private static String unique(final String one,
final String two)
{
final List<Character> base;
final Set<Character> toRemove;
final StringBuilder remaining;
base = new ArrayList<>(one.length());
toRemove = new HashSet<>();
for(final char c : one.toCharArray())
{
base.add(c);
}
for(final char c : two.toCharArray())
{
toRemove.add(c);
}
base.removeAll(toRemove);
remaining = new StringBuilder(base.size());
for(final char c : base)
{
remaining.append(c);
}
return (remaining.toString());
}
Iterate over the first string
For each character, check if the second string contains it
If it doesn't, add the caracter to a StringBuilder
Return stringBuilder.toString()

How to get string if length is more than 50?

In java how to split a string value, if >50 then the string after the last succeeding comma should be assigned to another String.
Eg:
String test = "ASDFGHJKLPOIUYTRE YUIOOPPKMABJFD AJDJDJDJD, DJDJDJD DJDJDJ, JDJDJD UYUYUAU JKBFDKJBDKJJK";
the above string's length is 88.
after 50th character #59th "," is presented so string should be split with the last succeeding "comma" and the output should be as follows:
ASDFGHJKLPOIUYTRE YUIOOPPKMABJFD AJDJDJDJD, DJDJDJD DJDJDJ,
JDJDJD UYUYUAU JKBFDKJBDKJJK
thanks in advance!!!
I have tried as follows:
if(add1.length() > 50){
for(int i=50;i<add1.length();i++){
if(add1.charAt(i)== ','){
add2 = add1.substring((i+1),add1.length());
add1 = add1.substring(0,i);
}
}
}
You can do this using the indexOf method of strings to find the next comma, then manually splitting:
if(test.length() > 50){
int comma = test.indexOf(',', 50);
if(comma >= 0){
//Bit before comma
String partOne = test.substring(0, comma);
//Bit after comma
String partTwo = test.substring(comma);
//Do Something
}
}
String someString = "";
int lastCommaPosition = someString.lastIndexOf(",");
if(lastCommaPosition > 50){
String firstPart = someString.substring(0,lastCommaPosition +1);
String secondPart = someString.substring(lastCommaPosition);
}
Try subString(), indexOf(), replace()
if(test.length() > 50)
{
System.out.println(test.substring(test.lastIndexOf(",") + 1, test.length()).replace(",", ""));
}
String test = "ASDFGHJKLPOIUYTRE YUIOOPPKMABJFD AJDJDJDJD, DJDJDJD DJDJDJ, JDJDJD UYUYUAU JKBFDKJBDKJJK";
int index = 0;
if (test.length() > 50) {
index = test.indexOf(",", 50);
}
String firstString = test.substring(0, index + 1);
String secondString = test.substring(index + 1);
System.out.println(firstString);
System.out.println(secondString);
// Java version
public class StrSub {
private String s = "ASDFGHJKLPOIUYTRE YUIOOPPKMABJFD AJDJDJDJD, DJDJDJD DJDJDJ, JDJDJD UYUYUAU JKBFDKJBDKJJK";
private void compute() {
int i = 50, p = 0, len = s.length();
String s1, s2;
for (i = 50; i < len; i++) {
if (s.charAt(i) == ',') {
p = i;
break;
}
}
System.out.println(p);
s1 = s.substring(0, p+1);
s2 = s.substring(p+1);
System.out.println(s1);
System.out.println(s2);
}
public static void main(String[] args) {
StrSub s = new StrSub();
s.compute();
}
}
Try this...
int index = 0;
String[] out= new String[test.length()/50]();
for(int i=50, j=0; test.length() > 50 ; j++){
if(test.length>i){
index = test.indexOf(",",i);
}
out[j] = test.subString(0,index);
test = test.subString(index+1, test.length());
}
// Cover the boundary condition
if(test.length() > 0){
out[j] = test;

Categories