Results 1 to 5 of 5
  1. #1
    Basit781 is offline Member
    Join Date
    May 2010
    Posts
    47
    Rep Power
    0

    Default how to repalce space with %20 in a string

    Hi,
    I have a TextArea in which user type any message. I get the string using textArea.getString like this.

    Java Code:
     String text = txtArea1.getText();
    Now i want if text contain any space then it replace with %20. I try the text.replace() method but the problem is it takes only character and i can't do it like.

    Java Code:
    text.raplace(" ", "%20");
    Because it's a string.In JAVASE there is replaceAll() method. But how can i do it here. Please guide me.

    Thanks

  2. #2
    Basit781 is offline Member
    Join Date
    May 2010
    Posts
    47
    Rep Power
    0

    Thumbs up

    Hi,
    I found the code. Here it is

    Java Code:
    public class ReplaceStr extends MIDlet {
    
    
    
    
        public ReplaceStr(){
            Display.init(this);
        }
    
        public void startApp() {
    
    
        String s = replace("My name is Basit", " ", "%20");
        System.out.println(s);
        }
    
        public void pauseApp() {
        }
    
        public void destroyApp(boolean unconditional) {
        }
    
        public final static String replace(String text, String searchString, String replacementString)
    {
        StringBuffer sBuffer = new StringBuffer();
        int pos = 0;
    
        while ((pos = text.indexOf(searchString)) != -1)
        {
            System.out.println("The pos is: " + pos);
            sBuffer.append(text.substring(0, pos) + replacementString);
            System.out.println("SearchString.length is: " +searchString.length());
            System.out.println("pos + searchString.length(): " + (pos + searchString.length()));
            text = text.substring(pos + searchString.length());
            System.out.println("The text is: " + text);
        }
    
        sBuffer.append(text);
        return sBuffer.toString();
    }
    }
    
    Thanks

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    There is another replace method in the String class which takes two CharSequences. Strings are CharSequences.
    So you only needed to do
    Java Code:
    String s = "My name is Basit".replace(" ", "%20");
    Of course if you followed advice posted to you you would know this because you would have the API specs with you as you write your code.

  4. #4
    manasabangre is offline Member
    Join Date
    Jun 2012
    Posts
    1
    Rep Power
    0

    Cool Re: how to repalce space with %20 in a string

    Hi,

    add reference to ur solution. system.web.

    then httpUtility.decode(txtname.txt);

    this vl provide space.

    Thanks,
    Manasa

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: how to repalce space with %20 in a string

    Quote Originally Posted by manasabangre View Post
    Hi,

    add reference to ur solution. system.web.

    then httpUtility.decode(txtname.txt);

    this vl provide space.

    Thanks,
    Manasa
    1. The question was asked 2 years ago. Basit wouldn't be waiting for your response for 2 years.
    2. Your response refers to an instance of some unidentifed and unidentifiable class that most certainly doesn't exist in the CLDC and MIDP API.
    3. Please go through the following: Forum Rules
    Guide For New Members
    BB Code List - Java Programming Forum

    4. Most importantly, this is a programming foruim -- not SMS chat. Please use proper words -- 'your' not 'ur' / 'will' not 'vl'

    Closing this ancient relic. Don't post to old dead threads again.

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Java split string, the first char is a space
    By new_cyber in forum New To Java
    Replies: 8
    Last Post: 12-31-2009, 01:27 AM
  2. Replies: 0
    Last Post: 11-17-2009, 01:58 PM
  3. Get item from string array and pack with blank space
    By firewalll in forum New To Java
    Replies: 2
    Last Post: 09-02-2009, 07:38 AM
  4. Replies: 0
    Last Post: 07-05-2009, 12:31 PM
  5. space between images !!
    By nhawlman in forum New To Java
    Replies: 1
    Last Post: 01-09-2009, 11:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •