How do i do this? I tried something like this but I do not seem to be able to get any further.
public void speak(String text)
{
String[] textArray = text.spl
For wrapping text, rather than focusing on the spaces, focus on the 100 character limit. For most text, spaces will occur far more often than once per 100 characters.
public void speak(String text) {
while(text.length() > 100) {
int nextSpace = text.lastIndexOf(" ", 99);
if (nextSpace == -1) { //no spaces for 100 characters
System.out.println(text.substring(0, 100));//give as many characters as possible, then split anyway
text = text.substring(100);
continue;
}
System.out.println(text.substring(0, nextSpace));
text = text.substring(nextSpace + 1);
}
System.out.println(text);
}
I know this answer is late and a bit inefficient but I think it does the job nicely.
public class StringSplitter
{
private int maxCharactersPerLine = 100;
public List<string> GetTextLines(string text)
{
var result = new List<string>();
if(text.Length <= this.maxCharactersPerLine)
{
result.Add(text);
return result;
}
string words[] = text.Split(new[]{' '}, StringSplittingOptions.RemoveEmptyEntries);
//accumolator, describes a line of text up to maximum character length.
string temp = string.Empty;
//this is so that we don't append an empty space on each new line.
bool isBeginingWord = true;
foreach(var word in words)
{
//there is a possibility that a text line has more than maximum
//consecutive characters without having a space. This is edge case.
if(temp.Length > this.maxCharactersPerLine)
{
result.Add(temp);
continue;
}
//if adding the next word in the list will exceed the
//maximum characters per line of text
if((temp + " " + word).Length > this.maxCharactersPerLine)
{
result.Add(temp); //add the accumolator to the list.
temp = ""; //reset the accumolator
isBeginingWord= true; //reset beginning of word flag.
continue;
}
//adding the next word from the list results in accumolator still
//still shorter than the maximum characters per line of text.
if(isBeginingWord)
{
temp = word;
isBeginingWord = false;
continue;
}
temp = temp + " " + word;
}
return result;
}
}
Is it possible with regular expression, it needs less code:
Pattern p=Pattern.compile("[\\s\\S]{99}");
Matcher matcher = p.matcher(s);
ArrayList<String> list= new ArrayList<>();
int lastFound=0;
while (matcher.find()){
list.add(matcher.group()+" ");
lastFound = matcher.end();
}
list.add(s.substring(lastFound));
If you mean something else just comment! May be you need to add more patterns in your string has tabs, new line and other special chars.
ArrayList<String> lines = new ArrayList<>();
int index = 0;
while(index < textArray.length()) {
StringBuilder builder = new StringBuilder();
if(textArray[index].length() >= 99) {
build.append(textArray[index]);
index++;
} else {
while(builder.length() < 99 - (textArray[index].length())) {
builder.append(textArray[index] + " ");
index++;
if(!(index < textArray.length()) {
break;
}
}
lines.add(builder.toString());
}
The inner while
loop will do what it's already doing in your code, build a single line, less than 100 characters long, adding a space after each word.
Then, when we would go over 100 characters, append a new line character to the string, and continue on.
As a note, this bit of code:
if(textArray[index].length() >= 99) {
build.append(textArray[index]);
index++;
}
will handle the (should-be-rare) case of a word which is longer than 99 characters (no spaces). As written, it will keep this word completely together on the line, and may result in a line longer than 100 characters. If you want different behavior, then look to this bit of code.
But words with more than 99 characters should probably be pretty rare and this may be a complete non-issue.