Replacing "PING" with "PONG" in string.

edited June 2016 in How To...

I am a newb to processing and have migrated from LiveCode. Just a quick question, not asking for a spoonfeed, just a general idea I should be looking at. The string contains PING and I am trying to replace it with PONG.

Many Thanks Guys!!

Matt.

Tagged:

Answers

  • Hey there. Well first you have to set your indicator. When do you want the string to change from Ping To Pong? Afterwards, say it's after a mousePressed. Then you would just do this:

    String PingOrPong = " ";
    
    void setup(){
    size(100,100);
    }
    
    void draw(){
    PingOrPong = "Ping";
    if(mousePressed){
    PingOrPong = "Pong";
    }
    println(PingOrPong);
    }
    

    Or if you want to permanently change it to Pong after mouse clicked....

    String PingOrPong = " ";
    
    void setup(){
    size(100,100);
    PingOrPong = "Ping";
    }
    
    void draw(){
    println(PingOrPong);
    }
    
    void mouseReleased(){
    PingOrPong = "Pong"; 
    }
    

    Or if you want to make it happen after a certain amount of time:

    String PingOrPong = " ";
    
    void setup(){
    size(100,100);
    PingOrPong = "Ping";
    }
    
    void draw(){
    println(PingOrPong);
    if(millis()>=3000){
      PingOrPong = "Pong";
    }
    }
    

    after 3 seconds in this case.

    So basically its all about resetting the initial value of your String variable. Hope it helps!

    ~TW7 :D

  • Thanks for your reply!! I meant to say that PING is among the characters of a string and i am trying to replace it in the string with PONG.

    Many Thanks,

    Matthew.

  • edited June 2016

    use str.indexOf("Ping") to find where "Ping" starts, then concatenate a new string from the bits before, the word Pong & the bits after.

        String PingOrPong = "helloPinghello ";
        int place = PingOrPong.indexOf("Ping");
        String newString = PingOrPong.substring(0, place);
        newString += "Pong";
        newString += PingOrPong.substring(place + 4, PingOrPong.length());;
        println(PingOrPong);
        println(newString);
    
  • Oh, hey there yes, the replies that were given are both what you're looking for, PingOrPong.indexOf is what you need. Sorry about missing the point, but i'm glad it got fixed.

  • edited June 2016 Answer ✓

    Thanks GoToLoop, that's much easier.

        String PingOrPong = "helloPinghello ";    
        String newerString = PingOrPong.replace("Ping", "Pong");
        println(newerString);
    
  • replace() will only do the first. replaceAll() will do all instances, should that be what you need.

  • String PingOrPong

    Java naming conventions would use pingOrPong here. Classes start with an uppercase letter, members with a lowercase letter

  • edited June 2016 Answer ✓

    replace() will only do the first.

    Alas! Please read the link before stating that: L-)
    http://docs.Oracle.com/javase/8/docs/api/java/lang/String.html#replace-java.lang.CharSequence-java.lang.CharSequence-

    Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".

Sign In or Register to comment.