Results 1 to 4 of 4
  1. #1
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default Removing words from a String

    I have a string e.g
    Java Code:
    String abc = "Sony Ericsson is one of the top, global, mobile phone manufacturers. Sony Ericsson is a joint venture established on October 1, 2001"
    Can you suggest me the best way to identify all the appearance of the word Sony Ericsson from the above string and replace it with Sony~Ericsson. So that the final o/p string is
    Java Code:
    String output= "Sony~Ericsson is one of the top, global, mobile phone manufacturers. Sony~Ericsson is a joint venture established on October 1, 2001"
    Last edited by nn12; 03-15-2011 at 10:13 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    Have a look at the String.replaceAll( ... ) method (read the API documentation for the String class).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default

    Yes Josah. I too was thinking of the same lines, but was not sure how to write an regex for the same. If anyone has prior experience with regex, can you help me out.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by nn12 View Post
    Yes Josah. I too was thinking of the same lines, but was not sure how to write an regex for the same. If anyone has prior experience with regex, can you help me out.
    The regular expression to search for is simply "Sony Ericsson" and the replacement String is "Sony~Ericsson"; nothing mysterious about them ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Removing last character from a String
    By newToJava3 in forum New To Java
    Replies: 2
    Last Post: 11-28-2010, 12:43 AM
  2. Replies: 8
    Last Post: 02-04-2009, 08:55 PM
  3. Replies: 4
    Last Post: 07-07-2008, 01:32 PM
  4. program help: Extracting words from a string
    By toad in forum New To Java
    Replies: 1
    Last Post: 11-04-2007, 06:39 PM
  5. Analyze a string of words
    By zoe in forum Advanced Java
    Replies: 2
    Last Post: 07-26-2007, 10:01 AM

Posting Permissions

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