How to remove extra spaces when text wraps?

Howdy, processing newbie here.

I'm using my processing program to pull strings from an XML file and display them, right aligned and wrapping when the string is too long. Unfortunately with them right-aligned, the first line of text has a space at the end of it that makes a visible indent when it wraps to the next line.

I'm trying to figure out how to get processing to check if the text is wrapping, and if so, remove the first blank space character from the string so that the weird indent doesn't happen. Ideally I'd like it to do this for each additional line wrapped (remove the spaces at the end of each line besides the last line, which has no space at the end).

Help would be appreciated!

color titleC = #58595B;
PFont titleFont;
float text1Base;
float transY = 400;

// Starting text, before text is pulled from the XML file upon user interaction. This also wraps to 2 lines
String startText1 = "WHAT DOES 2013 SAY?"; 
String text1;

void setup(){
  size(1200,800);
  frameRate(10);
  noStroke();
  text1 = startText1;
  titleFont = loadFont("Aachen-Bold-74.vlw"); // 74px size font
  text1Base = height-120-transY;  
}


void draw() {
  background(255);

  textFont(titleFont);
  textAlign(RIGHT);
  textLeading(80);
  fill(titleC);
  rectMode(CORNER);
  text(text1.toUpperCase(), (width-50), text1Base, -650, 300);

}
Tagged:

Answers

  • To remove start and end whitespace (spaces) from a String variable then use the trim e.g.

    text = text.trim();

  • Quark: Thanks, but that doesn't seem to be working for what I want it to do. The whitespace I'm trying to remove isn't at the start or end of the string being pulled from the XML file, but somewhere in the middle (wherever the text wrap happens). For example in my start text "What does 2013 say?" the whitespace I end up wanting to get rid of is between "does" and "2013".

  • How about splitTokens()?
    http://processing.org/reference/splitTokens_.html

    // forum.processing.org/two/discussion/3258/
    // how-to-remove-extra-spaces-when-text-wraps
    
    String sentence = "What does 2013 say?";
    String[] words = splitTokens(sentence);
    
    println(words);
    exit();
    
  • edited February 2014 Answer ✓

    run the code and you'll see what it's doing. it seems like the internal wrapping in the text() call that's the problem, and you might be out of luck.

  • edited February 2014

    GoToLoop: I see how it's split up the string in an array, but I'm not sure how to put that array back into the text() function and get rid of just the white space that's causing the indentations while preserving the other white spaces. I get how to put the string back without any whitespace at all, but that isn't what I was hoping to do.

    Koogs: Ah dang, so if it's the internal wrapping then I can't do anything about it?

Sign In or Register to comment.