Text, go to the next line after finding a certain character

Hi guys,
I’m going crazy on something really stupid, but I just can not solve it.
I would like to wrap after I’ve found a specific character.
the code I am using is this:

String[] lines; 
String randText;

void setup() {
  size(1280, 720);
  lines = loadStrings("howl.txt");
  background(0);
  frameRate(1);
}

void draw() {
  //fill(0, 255);
  //rect(0, 0, width, height);
  //background(0);

  randText=lines[frameCount].toUpperCase();

  for (int k=0; k<randText.length(); k++) {
    if (str(randText.charAt(k)).equals(" ")==true) {
      //randText.charAt(k)=; here i want to go to the next line
    }
  }
  text(randText, 10, frameCount*12);
}

thank you in advance!!

1 Like

ok…i’ve found this method StringBuffer, but now i want decide if a specific character have be to changed or not.
is there a way to do it?

String[] lines; 
String randText;
StringBuffer sb;

void setup() {
  size(1280, 720);
  lines = loadStrings("howl.txt");
  //ArrayList<StringBuffer> sb = new ArrayList<StringBuffer>();
  sb = new StringBuffer(lines.length);
  for (int i=0; i<lines.length; i++) {
    sb.append(lines[i]);
  }
  background(0);
  frameRate(1);
  noLoop();
}

void draw() {
  randText=sb.toString();
  randText=randText.replace(' ', '\n');
  text(randText, 10, frameCount*12);
}

for example i want to change just the second and fourth character " " not everyone, is it possible?

Yes with insert() method :

sb.insert(index,"your text to add")

Take a look : https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html

It’s not a method, but a class: :coffee:

But since Java 5, we shouldn’t use StringBuffer anymore! We use the faster StringBuilder class instead: :building_construction:

1 Like

Did you look at split () ?

Makes your string to an array!

Then you can for loop over it.

Also: text() is also available with 5 parameters, so you have automatically a line break. Nice.

2 Likes