Skip to content

Instantly share code, notes, and snippets.

@erjan
Created December 14, 2015 05:21
Show Gist options
  • Save erjan/b676d7b63c8a969932d8 to your computer and use it in GitHub Desktop.
Save erjan/b676d7b63c8a969932d8 to your computer and use it in GitHub Desktop.
split a long string but not in the middle of a word
private String[] split_string(String newQuestion){
/*
this:
"According to Ruzbihan, these 2 tribe
s were part of both uzbek and mangits"
must be:
"According to Ruzbihan, these 2 tribes
were part of both uzbek and mangits"
*/
String[] words = newQuestion.split(" ");
String first_part = "";
String second_part = "";
int size = 0;
for(int i = 0; (first_part.length() + words[i].length()) < newQuestion.length()/2 ; i++) {
size++;
first_part += words[i] + " ";
}
for(int j = size; j < words.length; j++) {
second_part += words[j] + " ";
}
return new String[]{first_part,second_part};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment