Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Lost at where to go with a problem I need to salve?

    I have to make a method that allows you to enter a string as a parameter, and then it removes all unnecessary output.
    I have no idea where to go on this I am not allowed to use the trim method that is built in.

    Example
    String str = " I like pie "

    String after method call
    "I like pie"

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    If you can't use trim, I'd presume you're not familiar with regex either. Also assuming that only the first or last chars being whitespace are relevant, you can simply iterate the chars in the string, and append them to another string after checking the char.
    The easiest way I can think of is to iterate the string up to string length or until a non-whitespace char is found (Character.isWhitespace can be used for this), which will give you the starting position of the first char that's not whitespace, then do the same thing but from the end of the string back to get the first non-whitespace char from the end. Then you simply make use of the String .substring method.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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