public static ArrayList<String> splitString(String s, int index){
ArrayList<String> result = new ArrayList<>();
StringBuilder constructor = new StringBuilder();
ArrayList<String> splitted = new ArrayList<>(Arrays.asList(s.split(" ")));
for(int i = 0;i<splitted.size();i++){
String word = splitted.get(i);
String nextWord = (i + 1 < splitted.size()) ? splitted.get(i + 1) : "";
int scen1 = constructor.length()+word.length();
int scen2 = scen1+1+nextWord.length();
if(scen2>index){
if(whichIsCloser(index, scen1, scen2)==scen1){
result.add(constructor+word);
constructor = new StringBuilder();
}else{
result.add(constructor+word+" "+nextWord);
splitted.remove(nextWord);
constructor = new StringBuilder();
}
}else {
constructor.append(word).append(" ");
}
}
result.add(constructor.toString());
return result;
}
public static int whichIsCloser(int base, int num1, int num2) {
int distance1 = Math.abs(base - num1);
int distance2 = Math.abs(base - num2);
if (distance1 < distance2) {
return num1;
} else {
return num2;
}
}```
Hello, newbie here.
So what the program does is it takes a string and splits it to multiple lines based on a character limit. If a character limit is reached, it will enter to a new line. But there is also a code that checks if it's more worth it to add the next word even if that next word exceeds total character limit, or to escape to new line
The runtime of this program is around 30ms
So is there any way to cut down the line of code for this program?
#is there a better way to do this split string program?
1 messages · Page 1 of 1 (latest)
Detected code, here are some useful tools:
Formatted code
public static ArrayList<String> splitString(String s, int index) {
ArrayList<String> result = new ArrayList<>();
StringBuilder constructor = new StringBuilder();
ArrayList<String> splitted = new ArrayList<>(Arrays.asList(s.split(" ")));
for (int i = 0; i < splitted.size(); i++) {
String word = splitted.get(i);
String nextWord = (i + 1 < splitted.size()) ? splitted.get(i + 1) : "";
int scen1 = constructor.length() + word.length();
int scen2 = scen1 + 1 + nextWord.length();
if (scen2 > index) {
if (whichIsCloser(index, scen1, scen2) == scen1) {
result.add(constructor + word);
constructor = new StringBuilder();
}
else {
result.add(constructor + word + " " + nextWord);
splitted.remove(nextWord);
constructor = new StringBuilder();
}
}
else {
constructor.append(word).append(" ");
}
}
result.add(constructor.toString());
return result;
}
public static int whichIsCloser(int base, int num1, int num2) {
int distance1 = Math.abs(base - num1);
int distance2 = Math.abs(base - num2);
if (distance1 < distance2) {
return num1;
}
else {
return num2;
}
}
<@&987246399047479336> please have a look, thanks.
I think this does something similar:
static String wordWrap(String str, int limit) {
String[] words = str.split(" ");
String line = "", lines = "";
for (String word : words) {
if (line.length() + word.length() <= limit) {
line += word + " ";
} else {
lines += line.trim() + "\n";
line = word + " ";
}
}
lines += line;
return lines.trim();
}```
Detected code, here are some useful tools:
Formatted code
static String wordWrap(String str, int limit) {
String[] words = str.split(" ");
String line = "", lines = "";
for (String word : words) {
if (line.length() + word.length() <= limit) {
line += word + " ";
}
else {
lines += line.trim() + "\n";
line = word + " ";
}
}
lines += line;
return lines.trim();
}
yes it does, however in the original code there is smth that checks if it's more worth it to add the next word even if that next word exceeds total character limit, or to escape to new line
it does that by checking if adding next word is closer to limit or not adding next word
maybe add something that checks that in the else block
you also need to consider if the word is longer than limit
like if limit is 3 and the word is hello
would be something like
if (line.length() + word.length() <= limit) {
line += word + " ";
}
else {
if (isCloserToLimit(word, line, limit)) {
line += word + " ";
lines += line.trim() + "\n";
line = "";
} else {
lines += line.trim() + "\n";
line = word + " ";
}
}```
Detected code, here are some useful tools:
Formatted code
if (line.length() + word.length() <= limit) {
line += word + " ";
}
else {
if (isCloserToLimit(word, line, limit)) {
line += word + " ";
lines += line.trim() + "\n";
line = "";
}
else {
lines += line.trim() + "\n";
line = word + " ";
}
}
isCloserToLimit() being a boolean that checks if its worth adding