We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Here is what I need to fix:
String fact = "blah blah blah blah blah blah blah blah blah blah blah blah"; String[] flines; text(fact, 500, 315, 440, 140); flines = split(fact, "\n"); println(flines.length);
How can I count the number of line feeds in my string?
Answers
Your example string doesn't contain any line breaks; so this question at first appears confusing. Perhaps the problem is that whatever String you're really trying to process doesn't contain \n but instead contains \r. This appears to be a common problem.
Since different systems define line breaks in different ways you have to check for both types. The accepted answer on the above link gives a solution for Java's split() method. I expect you can do something similar using Processing's splitTokens. Untested but perhaps this will work (not sure if you have to escape the backslash as in pure Java):
Sorry, this doesn't work. For some reason, it always prints that there's only one line. :( To specify, the string has x and y boundaries, so I don't know at what point to put the split().
I think you have 'spaces' not line breaks. Not
'\n'
nor'\r'
, but' '
.Anyway, Processing has this handy constant: WHITESPACE.
Also as @blindfish said
splitTokens
. check the link he posted.try this:
Just an extra trick: splitTokens() already take care for any
char
considered WHITESPACE by default:https://Processing.org/reference/splitTokens_.html
So
flines = splitTokens(fact);
is enough! ;)Text with 5 params makes line breaks.
But it never tells us where
It is a blackbox
So although the string doesn't have any returns in it, it automatically returns because of the x boundary. I'm trying to count the automatic returns.
You can then calc them manually... I've made an attempt to make a twitter text box.
This here, is not the complete code ( won't compile as it is), just the part splitting and creating lines. There is a lot more as I was working to make links and mention clickable.
Also there is a lot of code to deal with words bigger than the box width, if you are creating the words, you don't need this.
Although they are very commented and should be easy to follow, I hope.
disclosure: I don't really know what I'm doing... :)
Guys, thanks for all the help, but it still doesn't work. I need to know how to count the automatic line breaks in my string.