Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Unhappy deleting spaces !!

    Hello everybody,
    I was writing a code in java that allows to deleting superfluous spaces from a string.
    The problem is that the system refuses this instruction :
    ch.charAt(i)=ch.charAt(i+1);
    where ch is the string.
    how can i replace this instruction ??

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    What is the error you are getting?

  3. #3
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    The left-hand side of an assignment must be a variable

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    There you go then.

    And, to save you some trouble looking for a setCharAt() method, Strings are immutable...that is they cannot be changed.

    StringBuilder might help there.

  5. #5
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    thanks a lot Tolls :)
    the code is right now ;)
    here is my code :
    public class main2 {
    public static void main(String[] args) {

    // delete extra spaces
    StringBuffer ch=new StringBuffer(100);
    int i =0;
    ch.append(" g d ");
    if (ch.charAt( 0)==' ')

    ch.deleteCharAt(0);
    if(ch.charAt(ch.length()-1)==' ')
    ch.deleteCharAt(ch.length()-1);

    while (i<ch.length()-1)
    {
    if ((ch.charAt(i)==' ')&&(ch.charAt(i+1)==' '))
    {

    ch.deleteCharAt(i+1);

    }
    else
    i++;
    }

    System.out.println(ch);
    }

    }

  6. #6
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    You can use RegExp.
    Java Code:
    String text = "Hello world";
    text = text.replaceAll("\\s+", "");
    Skype: petrarsentev
    http://TrackStudio.com

  7. #7
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    text = text.replaceAll("\\s+", "");
    \\s+ -->what do you mean Petr ??

  8. #8
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

Similar Threads

  1. Extra spaces in JLabel...Please help..
    By sohamde in forum New To Java
    Replies: 6
    Last Post: 12-06-2010, 03:53 AM
  2. Replace a cell with Spaces
    By Shyamz1 in forum New To Java
    Replies: 11
    Last Post: 11-07-2010, 02:37 PM
  3. problem with spaces in
    By olli_m in forum IntelliJ IDEA
    Replies: 0
    Last Post: 11-24-2009, 05:55 PM
  4. How do i add spaces ~(very simple)
    By soc86 in forum New To Java
    Replies: 3
    Last Post: 11-02-2008, 02:01 AM
  5. Load URL that contains spaces?
    By barkster in forum Java Applets
    Replies: 0
    Last Post: 01-30-2008, 09:40 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
  •