We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › splitTokens() and linebreaks
Page Index Toggle Pages: 1
splitTokens() and linebreaks (Read 522 times)
splitTokens() and linebreaks
Jun 21st, 2008, 6:38pm
 
Hello,

How can I use splitTokens() to chop up a string using only linebreaks as tokens? The help file is a bit vague on this, saying

"If no tokens character is specified, a whitespace character is used as the split character. Whitespace characters include tab (t), line feed (n), carriage return (r), and form feed (f), and space."

I've tried the following:
array = splitTokens(string, n); // because I want to use linefeeds

What's the correct syntax for this?

Thanks in advance for your help.

Re: splitTokens() and linebreaks
Reply #1 - Jun 21st, 2008, 6:50pm
 
It looks like the reference might be missing some \ characters. The correct usage is:

splitTokens(string,"\n");

'\n' is the code for "newline", '\t' = tab etc...
Re: splitTokens() and linebreaks
Reply #2 - Jun 21st, 2008, 7:10pm
 
I just quickly tried splitTokens(string, "/n") and it doesn't seem to work as expected in my code. (the code is long and complex, as it next tries to trim out a specific string from a long HTML page, then clearing out the tags, then converting into Morse code etc). The above seems to make my code strip out all the 'n's from the string, but I'll have to dig into it to find out why.

Also tried '/n' and "\n", which also don't act as expected.
Any further assistance greatly appreciated!
m
Re: splitTokens() and linebreaks
Reply #3 - Jun 22nd, 2008, 9:31am
 
"/n" is two characters: / and n.
"\n" is indeed new line in Java.
'/n' probably generates a syntax error.
To strip out tags out of HTML, you should use an HTML parser instead, it is probably more reliable (taking in account full syntax).
The Libraries page lists proHTML, I haven't tried it yet but it might do the job.
Re: splitTokens() and linebreaks
Reply #4 - Jun 22nd, 2008, 1:15pm
 
Thanks for this - "\n" now seems to be working, after a little debugging of the rest of my code.

I did take a look at proHTML, but it seemed like overkill for my specific application, which only needs to lift a small section of plain text from a larger HTML document.

Thanks for the help everyone!
Page Index Toggle Pages: 1