Reading Strings

edited October 2014 in How To...

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

  • edited September 2014

    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: ;;)

    // forum.processing.org/two/discussion/7372/reading-strings
    
    static final CharSequence lastChars(CharSequence s, int n) {
      int len = s.length();
      return s.subSequence(Math.max(0, len - Math.abs(n)), len);
    }
    
    void setup() {
      String txt = "Hello World";
      println(lastChars(txt, 3));
      exit();
    }
    
  • String lastChars(String s, int n) {
      int len = s.length();
      return s.substring(max(0, len - abs(n)), len);
    }
    

    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

  • edited September 2014

    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

  • and is less confusing to newbies, using only types explained in the Reference.

    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:-)

    // forum.processing.org/two/discussion/7372/reading-strings
    
    static final <S extends CharSequence> S lastChars(S s, int n) {
      int len = s.length();
      return (S) s.subSequence(Math.max(0, len - Math.abs(n)), len);
    }
    
    void setup() {
      String txt  = "Hello World";
      String last = lastChars(txt, 3);
      println(last);
      exit();
    }
    
  • Why not this? ;)

    String lastChars(String s, int n) {
      int len = s.length();
      return s.substring(max(0, len - abs(n)));
    }
    
    void setup() {
      String txt  = "Hello World";
      String last = lastChars(txt, 3);
      println(last);
      exit();
    }
    
  • edited September 2014

    Since substring() we don't always need its 2nd argument, you can be more direct: ;))

    return s.substring(max(0, s.length() - abs(n)));
    
  • Answer ✓

    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 :)

    String lastChars(String s, int n) {
      return s.substring(max(0, s.length() - abs(n)));
    }
    
    void setup() {
      String txt  = "Hello World";
      String last = lastChars(txt, 3);
      println(last);
      exit();
    }  
    
  • "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... :-)

  • edited October 2014
  • Answer ✓

    Yes, but it doesn't know CharSequence, AFAIK.

  • edited October 2014 Answer ✓

    Actually, JS doesn't even know what a char is! But that's irrelevant, b/c JS variables don't have a type! :P
    Although 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

    // forum.processing.org/two/discussion/7372/reading-strings
    // by GoToLoop (2014-Oct)
    
    static final <S extends CharSequence> S lastChars(S s, int n) {
      int len = s.length(), idx = Math.max(0, len - Math.abs(n));
      return 1/2 == 0
        ? (S) s.subSequence(idx, len)
        : (S) ((String) s).substring(idx);
    }
    
    void setup() {
      String txt  = "Hello World";
      String last = lastChars(txt, 3);
      println(last);
      exit();
    }
    
  • 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:

    String firstChars(String s, int n) {
      return s.substring(0, s.length() - abs(n - s.length()));
    }
    
  • edited October 2014

    My adapted version for firstChars(): <:-P

    // forum.processing.org/two/discussion/7372/reading-strings
    // by GoToLoop (2014-Oct)
    
    static final <S extends CharSequence> S firstChars(S s, int n) {
      int idx = Math.min(s.length(), Math.abs(n));
      return 1/2 == 0
        ? (S) s.subSequence(0, idx)
        : (S) ((String) s).substring(0, idx);
    }
    
    void setup() {
      String txt  = "Hello World";
      String last = firstChars(txt, 3);
      println(last);
      exit();
    }
    
Sign In or Register to comment.