Results 1 to 6 of 6

Thread: Managing space in string

  1. #1
    Join Date
    Dec 2009
    Posts
    296

    Managing space in string

    Hello,
    I am new to java and I am trying to do a program where I can eliminate the spaces in the String. I have a variable of type String and I want to delete all the spaces it contains. How to manage the space in the String. If you have any ideas about this then please do post back. An sample code will help me a lot. Thanks in the advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    446

    Re: Managing space in string

    Hello,
    I think this is simple and you can do it quit easily. Here is the part of the code for doing the same .
    Code:
    String s = "sample test";
    s = s.replace (" ", "");
    I think this part of code should do the trick for you. I think there are some methods in java which you can use directly and perform the same thing. But I have no idea about those.

  3. #3
    Join Date
    Dec 2009
    Posts
    213

    Re: Managing space in string

    Hello,
    I have a similar kind of a problem so, if you have any idea to how to do it then please help me. This is what I try , here is my code
    Code:
    	Public static String remsp(String s) {
    		System.out.System.out.println("before"+ s);
    String s = s.replaceAll(" ","");
    s = s.replaceAll("\\s + ", "");
    System.out.System.out.println("after"+ s);
    		return s;
             }
    If you find any problem in the code then please guide.

  4. #4
    Join Date
    Dec 2009
    Posts
    296

    Re: Managing space in string

    Hello,
    I also tried without success the following code:
    Code:
    StringTokenizer strtok = new StringTokenizer(s" ",false);
    String t ="";
    		  while (strtok.hasMoreElements()) + t = strtok.nextElement();
    		  return t;
    Ideas are welcomed, thank you in advance. If you have any other code with you then please place it here and it will help me.

  5. #5
    Join Date
    Nov 2009
    Posts
    343

    Re: Managing space in string

    Hello,
    The replaceAll ( "\ \ s +" ") should normally be sufficient. I think the problem is that you are using a NumberFormat to format strings, and that French is used as a separator thousand non-breakable space, which is not included in the expression \ \ s. I think you should try this in your code and see if the code works for you. If not thin then there are many alternatives available for you, but first try this option.

  6. #6
    Join Date
    Nov 2009
    Posts
    347

    Re: Managing space in string

    Hello,
    Yes, even I believe in the above post and I have solutions for doing this
    Two solutions:
    1. Either you use the method setGroupingUsed (false) to prevent the NumberFormat to use the thousand separator.
    2. Either you add the indivisible character in your expression:
    Here is the code for it
    Code:
    	Public static String remsp(String s) {
    		return s.replaceAll("[\\s\ u00b0] + ", "");
    	}

Similar Threads

  1. Replies: 11
    Last Post: 06-09-2011, 10:16 AM
  2. Decrease space in Recycle Bin to increase storage space
    By Leonard in forum Customize Desktop
    Replies: 7
    Last Post: 04-10-2010, 04:01 PM
  3. Deleting space in a string in C programming
    By Ameeryan in forum Software Development
    Replies: 4
    Last Post: 09-03-2010, 11:01 PM
  4. How to find string that doesn't start with empty space?
    By MACE in forum Software Development
    Replies: 4
    Last Post: 02-03-2010, 06:21 PM
  5. How to Manipulate String using PHP String Functions
    By ComPaCt in forum Software Development
    Replies: 3
    Last Post: 21-09-2009, 09:07 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,265,470.47133 seconds with 17 queries