Making a line break instruction in a string work even though it's fetched from a csv file

I’m trying to print a string with a line break in it, where this string is fetched from a csv file.

When running the code below, typing in the string with the line break myself, everything works as expected:

import processing.pdf.*;
PFont f;

void setup() 
{  
    size(1000,1000, PDF, "Output.pdf");
    background(255);
    f = createFont("Arial",16);
}

void draw() 
{  
    Table table;
    table = loadTable("Input.csv", "header");
    for (int i = 0; i < table.getRowCount(); i = i + 1)
    {    
        textFont(f);
        fill(0);
        text("First line.\nSecond line.", 100, 100); // I'm typing in the string myself here.
    }
    exit();
}

However, when running this other piece of code below, fetching the string with the line break from a csv file, the line break instructions, “\n”, is printed and no line break occurs:

import processing.pdf.*;
PFont f;

void setup() 
{  
    size(1000,1000, PDF, "Output.pdf");
    background(255);
    f = createFont("Arial",16);
}

void draw() 
{  
    Table table;
    table = loadTable("Input.csv", "header");
    for (int i = 0; i < table.getRowCount(); i = i + 1)
    {    
        textFont(f);
        fill(0);
        text(table.getString(i,"Column_1"), 100, 100); // I'm fetching the string from the csv file here.
    }
    exit();
}

This is what the csv file looks like:

"Column_1"
"First line.\nSecond line."

Interesting!

Can you give an example line from the csv?

Presumably the Strings in it must be enclosed in " and "

Sorry, I forgot to add the csv file. I’ve added it at the end of my post. As you can see, the string is enclosed in quotes.

Hi @Bobolink ,

Welcome to the forum! :wink:

I found the issue, I made a small code to show the string as an array of characters :

String firstRow = table.getString(0, "Column_1");
  
print("[");
for (int i = 0; i < firstRow.length(); i++) {
  print("'" + firstRow.charAt(i) + "'");
  if (i < firstRow.length() - 1) print(", ");
}
print("]");

Which gives :

['F', 'i', 'r', 's', 't', ' ', 'l', 'i', 'n', 'e', '.', '\', 'n', 'S', 'e', 'c', 'o', 'n', 'd', ' ', 'l', 'i', 'n', 'e', '.']

As you can see, the line break is considered as two separate characters therefore they are drawn when using the text() function.

Here is the solution :

You need to put a real line break inside the quotes :

"Column_1"
"First line.
Second line."

Thanks, that solved it! :slight_smile: I still don’t fully understand why the string which is typed in manually wasn’t treated the same way, but that’s not that important now I guess.

2 Likes

Interesting.

But I don’t think I can do this in the normal Windows Notepad Editor?

Can I do it in Excel? It’s Alt-Return or something.

So which editor to use?

When the csv is made from another Sketch it’s different maybe