Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: String replacement

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Thanked 2 Times in 2 Posts

    Default String replacement

    Every example I have found tells me the following should replace brackets with spaces

    		String lineIn = "[10/Feb/2011 15:56:27 -0000]";
    		lineIn.replace('[', ' ');
    		lineIn.replace(']', ' ');

    Yet my output is this.
    [10/Feb/2011 15:56:27 -0000]

    Do I really have to build out a regular expression an pattern match these? (BTW I tried that unsuccessfully also).

    I appreciate any help. It doesn't seem like this should be that hard.


    Last edited by helloworld922; February 28th, 2011 at 11:10 PM.

  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Thanked 619 Times in 561 Posts
    Blog Entries

    Default Re: String replacement

    In java, strings are immutable (i.e. can't be modified/changed). The replace() method for strings actually creates a new string with the characters replaced. Simply assign the result of replace() back into lineIn.

    lineIn = lineIn.replace('[', ' ');
    lineIn = lineIn.replace(']', ' ');

    As a side note please use highlight tags around your code.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    codethrupain (February 28th, 2011)

Similar Threads

  1. Replies: 18
    Last Post: March 2nd, 2011, 10:52 AM
  2. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 09:58 AM
  3. "?" replacement
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 10th, 2011, 10:13 PM
  4. Replies: 2
    Last Post: December 22nd, 2010, 09:21 AM
  5. Page Replacement Algorithms
    By smokeyjoey in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 7th, 2009, 10:38 PM

Tags for this Thread