We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, How would one take a set string (e.g. "Hello World"), determine the last few letters in the string (e.g. "rld"), and then return those values as another string?
Answers
You shoulda at least made an effort before asking, right? 8-|
Since it was a quick 1, I've come up w/ a lastChars() util function: ;;)
also works and is less confusing to newbies, using only types explained in the Reference.
hey
Before some time i go through below site which is a excellent tool for formatting text.
www.textformat.in
Thanks
We can omit the 2nd argument in substring():
return s.substring(max(0, s.length() - abs(n)));
http://processing.org/reference/String_substring_.html
I couldn't do that for the more "neolithic" subSequence() though! :o3
But at least my version is guaranteed to work everywhere in Java. Not bound to Processing's API! ;;)
Also, it can accept other unknown String siblings too: :>
docs.oracle.com/javase/8/docs/api/java/lang/CharSequence.html
Anything that reduces the learning curve for newbies is a good thing. Why bombard them with unnecessary complications from the world of Java. Plenty of time for that when they become more proficient at programming and want to get beyond the scope of Processing.
A little correction so it's allowed direct assignment from returning result: O:-)
Why not this? ;)
Since substring() we don't always need its 2nd argument, you can be more direct: ;))
look at my last comment again - what second argument? ;))
Excellent so we should all be able to agree on :)
"at least my version is guaranteed to work everywhere in Java. Not bound to Processing's API!"
I don't see what you mean by that. substring() isn't part of Processing API, so I must misunderstand your message.
There was Processing's abs() & max()! :O
Ah, OK. Let's say your version is perfect for Java, and mine is better for JS compatibility, then... :-)
Math "class" is a global JS built-in object too: :-bd
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math
Yes, but it doesn't know CharSequence, AFAIK.
Actually, JS doesn't even know what a
char
is! But that's irrelevant, b/c JS variables don't have a type! :PAlthough I was expecting you to mention the "unknown" method subSequence(). Which doesn't exist in JS! 3:-O
But w/ an extra "ternary" operator, that can be arranged and turned into a working cross Java-JS mode: :)>-
processing.org/reference/conditional.html
Thanks, sorry I have not been able to check the posts. This is great. I was entirely baffled before. :)
Based on that code, here is a firstChars() util function:
My adapted version for firstChars(): <:-P