We are about to switch to a new forum software. Until then we have removed the registration on this forum.
This is part of a larger project, but I've been trying to put lowercase, uppercase, and "" in its own string and combine it with the string text title, but it just puts it at the end of text
String headLine="text1";
String body="text2";
String details="text3";
String[] cases= {".toLowerCase()", ".toUpperCase()", ""};
int index = int(random(cases.length));
text (body + cases[index], int(random(50,300)), int(random(0,600)), int(random(400,700)), height);
Answers
What you have in that array is a list of names of methods, you can't apply them dynamically like that, Java doesn't do that.
I would pick a random number from 1 to 3 and use this in a switch statement, calling the methods in the relevant case statement.
Wow in my combing of the reference library didn't even see switches. Thanks!
@gmunz --
To extend @GoToLoop 's solution slightly, if this was a function, how would you extend it to also randomly return titlecase? (Titlecase Is Written Like This).
Unlike
toLowerCase()
andtoUpperCase()
, which are built-in, in order to implement titlecase you might need to manually loop over the characters in the string, check if the character to the left is a letter or not, and capitalize them if not.Since P3, the pre-compiler supports
enum
keyword for ".pde" tabs too. \m/I think this case is a gr8 opportunity to toy w/ it: :bz
@GoToLoop -- very cool -- I didn't know enums were allowed, and nice StringBuilder!
(Forum needs @ Override to be cheated to make the code readable.)
DONE! >:)
Actually my Case.TITLE.change() was based on this sample code: :-\"
http://StackOverflow.com/a/1086134
Although I've tweaked it a little. :ar!
His relies on append(). While mine goes w/ setCharAt():
http://docs.Oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
Added a
static
method toenum
Case called randomCase(): O:-)