Results 1 to 7 of 7
  1. #1
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Removing spaces in String ArrayList

    Hi,

    Id like to know how to remove multiple spaces in each String and to allow only one space between each, in an ArrayList.

    For example if I had three String variables shown below.
    (THE UNDERLINE BETWEEN EACH NUMBER ARE SUPPOSE TO BE SPACES)

    String line1 = "_one____two_________three";
    String line2 = " ______one___two______three";
    String line3= "_one__two________three";

    And then to format each String element in the list to allow only one space each?. Shown below

    Envisioned Solution.

    Line 1: "one two three";
    Line 2: "one two three";
    Line 3: "one two three";

    Thanks
    Last edited by dougie1809; 03-13-2012 at 08:17 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Removing spaces in String ArrayList

    Look at the String class's replace... methods. I think the one that uses a regular expression will do what you want.

    The leading and trailing spaces can be trimmed.

  3. #3
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Removing spaces in String ArrayList

    Ive tried using the replaceAll method but it doesn't seem to work at all.

    Plus how can you replace spaces with one space if the multiple spaces are arbitrary? What would the reg exp will be?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Removing spaces in String ArrayList

    Try this for a regexp: \\s+

  5. #5
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Removing spaces in String ArrayList

    Yea that seems like the right solution, but it it still results the same.

    Here is the sample code below:
    Java Code:
    public static void main(String args [])
    	{
    		String line1 = "  one     two          three";
    	
    		System.out.println("Old: "+line1);
    		
    		line1.replaceAll("\\s+", " ");
    		
    		System.out.println("New: "+line1);
    	}
    The old and new output are still the same?
    Last edited by Norm; 03-13-2012 at 08:59 PM. Reason: The line1 should be alot more spaced out

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Removing spaces in String ArrayList

    Please post the results. Be sure to include it in code tags to preserve the spaces.
    The old and new output are still the same?
    You are comparing the old with the old.

    Remember that Strings are immutable. You can not change the contents of a String. Look at the method you are using.
    Last edited by Norm; 03-13-2012 at 09:01 PM.

  7. #7
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Removing spaces in String ArrayList

    Thanks. Yea I see where I went wrong.
    Thanks again

Similar Threads

  1. Replies: 1
    Last Post: 09-15-2011, 01:51 PM
  2. [HELP] Removing objects from an ArrayList!
    By bpx95 in forum New To Java
    Replies: 4
    Last Post: 04-30-2011, 05:54 AM
  3. removing repeated entries in arraylist
    By ankit1801 in forum New To Java
    Replies: 1
    Last Post: 04-15-2011, 06:34 AM
  4. Removing from an ArrayList while looping thru
    By pahiker in forum New To Java
    Replies: 7
    Last Post: 06-22-2010, 07:38 AM
  5. How to display a String that contains more than one white spaces
    By thachun in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 09-27-2009, 09:16 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
  •