4 Replies - 327 Views - Last Post: 02 December 2012 - 09:56 AM Rate Topic: -----

#1 rockkrose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 25-November 12

Remove unwanted space from string(C# begginer)

Posted 02 December 2012 - 07:24 AM

Hello

I have this method:
  public static string Puzzle(int upperBound) {
    string result = " ";
    for (int i = upperBound; i >=1; i--) {
      result = i*i+ " " + result;
      }
    return result;
      }


for upperBound = 7; result should be: "0 1 4 9 16 25 36 49" but in my case is "0 1 4 9 16 25 36 49 " it contains a space at the end of the string. I don't know how to remove only that space and still add a space between the letters. I would appreciate any ideas. Thanks!

This post has been edited by rockkrose: 02 December 2012 - 07:27 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Remove unwanted space from string(C# begginer)

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Remove unwanted space from string(C# begginer)

Posted 02 December 2012 - 07:26 AM

return result.Trim();


String.Trim()
Was This Post Helpful? 2
  • +
  • -

#3 rockkrose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 25-November 12

Re: Remove unwanted space from string(C# begginer)

Posted 02 December 2012 - 07:33 AM

View PostRyano121, on 02 December 2012 - 07:26 AM, said:

return result.Trim();


String.Trim()


Problem solved! thanks!
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3452
  • View blog
  • Posts: 10,651
  • Joined: 05-May 12

Re: Remove unwanted space from string(C# begginer)

Posted 02 December 2012 - 08:59 AM

I have to ask. With the code you posted above, how are you getting "0 1 4 9 16 25 36 49 "?

I can see the code producing " 49 36 25 16 9 4 1 ". The zero is not even going to be part of the sequence.
Was This Post Helpful? 0
  • +
  • -

#5 rockkrose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 25-November 12

Re: Remove unwanted space from string(C# begginer)

Posted 02 December 2012 - 09:56 AM

View PostSkydiver, on 02 December 2012 - 08:59 AM, said:

I have to ask. With the code you posted above, how are you getting "0 1 4 9 16 25 36 49 "?

I can see the code producing " 49 36 25 16 9 4 1 ". The zero is not even going to be part of the sequence.


Yes it's true, "i" should start from 0, but the order is correct because "i" is decremented. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1