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
enumkeyword for ".pde" tabs too. \m/I think this case is a gr8 opportunity to toy w/ it: :bz
/** * Enum String Cases (v1.0.1) * GoToloop (2016-Nov-14) * * https://forum.Processing.org/two/discussion/19014/ * how-to-randomly-fluctuate-between-lowercase-uppercase- * and-originally-formatted-text-formatting#Item_5 */ static final String SAMPLE = "LOREM IpsUm sit doloR."; void setup() { for (int i = 0; i < 5; ++i) { final int idx = (int) random(Case.length); final Case mode = Case.array[idx]; final String txt = mode.change(SAMPLE); println(mode + ":\t" + txt); } exit(); } enum Case { ORIG { @Override String change(final String s) { return s; } }, LOWER { @Override String change(final String s) { return s.toLowerCase(); } }, UPPER { @Override String change(final String s) { return s.toUpperCase(); } }, TITLE { // http://StackOverflow.com/a/1086134 @Override String change(final String s) { final StringBuilder titled = new StringBuilder(s); final int len = titled.length(); boolean nextCharIsTitle = true; for (int i = 0; i < len; ++i) { final char ch = titled.charAt(i); if (Character.isSpaceChar(ch)) nextCharIsTitle = true; else if (nextCharIsTitle) { nextCharIsTitle = false; titled.setCharAt(i, Character.toTitleCase(ch)); } } return titled.toString(); } }; static final Case[] array = Case.values(); static final int length = array.length; abstract String change(final String s); }@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
staticmethod toenumCase called randomCase(): O:-)/** * Enum String Cases (v2.2.1) * GoToloop (2016-Nov-14) * * https://forum.Processing.org/two/discussion/19014/ * how-to-randomly-fluctuate-between-lowercase-uppercase- * and-originally-formatted-text-formatting#Item_9 */ static final String SAMPLE = "LOREM IpsUm sit doloR."; void setup() { for (int i = 0; i < 5; ++i) { final Case mode = Case.randomCase(this); final String txt = mode.change(SAMPLE); println(mode + ":\t" + txt); } exit(); } enum Case { ORIG { @Override String change(final String s) { return s; } }, LOWER { @Override String change(final String s) { return s.toLowerCase(); } }, UPPER { @Override String change(final String s) { return s.toUpperCase(); } }, TITLE { // http://StackOverflow.com/a/1086134 @Override String change(final String s) { final StringBuilder titled = new StringBuilder(s); final int len = titled.length(); boolean nextCharIsTitle = true; for (int i = 0; i < len; ++i) { final char ch = titled.charAt(i); if (Character.isSpaceChar(ch)) nextCharIsTitle = true; else if (nextCharIsTitle) { nextCharIsTitle = false; titled.setCharAt(i, Character.toTitleCase(ch)); } } return titled.toString(); } }; static final Case[] array = Case.values(); static final int length = array.length; static final Case randomCase() { final int idx = (int) (Math.random() * length); return array[idx]; } static final Case randomCase(final PApplet p) { final int idx = (int) p.random(length); return array[idx]; } abstract String change(final String s); }