text - How do I replace line breaks with spaces in Processing? -


i imported txt file processing. replace line breaks imported text file spaces. possible using processing?

a couple of things... since imported txt file, guess loadstrings() method assume left array of strings. mean 2 things, either join array 1 string or find , replace line breaks (or combination). lets see both possibilities.

first assume similar string array loadstrings() gives you:

string [] importedtext = {"aaaa bbb","cccc dddd","eeee ffff gggg"}; 

if want join array 1 single string, iterate on array , add elements string so:

string singlelinetext = "";  for(int = 0;i < importedtext.length; i++) {    singlelinetext += importedtext[i] + " "; } 

this simplest , less confusing way it, unless have huge array of strings , after performance (a debatable matter these days) can use stringbuilder this:

stringbuilder strb = new stringbuilder(); for(int = 0;i < importedtext.length; i++) {    strb.append(importedtext[i]);    strb.append(" "); } string singlelinetext = strb.tostring(); 

if on other hand after removing line breaks apologize useless prologue , here way: there two types of line breaks (two characters), have account for: line-feed(\n) , carriage-return(\r). need account both because different operating systems use different breaks. lets suppose string this:

string linewithbreaks = "aaaa\nbbbbb\rcccc\n\rddddd"; 

as our 2 breaks:

string linefeed = "\n"; string carriagereturn = "\r"; 

the method remove breaks this:

linewithbreaks = linewithbreaks.replace(linefeed," "); linewithbreaks = linewithbreaks.replace(carriagereturn," "); 

where replace 2 set strings space character " "!


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -