4 Replies - 5867 Views - Last Post: 07 May 2010 - 10:09 PM Rate Topic: -----

#1 huma samrin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-April 08

REMOVE leading Spaces in String Array

Posted 07 May 2010 - 11:12 AM

Hello,

//I am using devExpress' combo box in my project
//i passed its in the string list as

string[] list=cmb.editvalue.tostring();
the out put is like that ('A A',' BBB',' C CC' etc)

Now i want to remove only the leading space in each element of the array i try this ;
plz guide me if there any function to remove the spaces in array

i use
list.replace("",empty.string)
but it removed the spaces between elements as well
how can i get the output ('A A','BBB','C CC')


Thanx in advance

Is This A Good Question/Topic? 0
  • +

Replies To: REMOVE leading Spaces in String Array

#2 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: REMOVE leading Spaces in String Array

Posted 07 May 2010 - 11:56 AM

Use the Trim method:

// Loop through each string.
for (int i = 0; i < list.Length; i++)
{
    // Remove any leading/trailing whitespace characters.
    list[i] = list[i].Trim();
}



Alternatively, you could use TrimStart.

EDIT: Needed to fix the code. :)

This post has been edited by lesPaul456: 07 May 2010 - 12:02 PM

Was This Post Helpful? 1
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1619
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: REMOVE leading Spaces in String Array

Posted 07 May 2010 - 11:59 AM

You could use string.TrimStart. Here's an example (just for simplicity sake I just load them into a ListView

private void TrimStartExample()
{
    string[] sample = new string[] { "A A", " BBB", " C CC" };

    foreach (string s in sample)
    {
        ListView1.Items.Add(s.TrimStart(new char[] { ' ' }));
    }
}


Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#4 MentalFloss  Icon User is offline

  • Not really an expert anymore...
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,157
  • Joined: 02-September 09

Re: REMOVE leading Spaces in String Array

Posted 07 May 2010 - 04:29 PM

Quote

ListView1.Items.Add(s.TrimStart(new char[] { ' ' }));



Maybe you guys won't be in agreement with me, but since this is a parameter and we know that the idea behind trimming for the most part is removing whitespace, I think it's better to omit the char array.

ListView1.Items.Add(s.TrimStart());



I just think it looks cleaner and since it's not necessary, why add it?

EDIT: when I said "but since this is a parameter", I mean that it's a params parameter, not just the fact that it's a method parameter. Sorry for confusion.

This post has been edited by MentalFloss: 07 May 2010 - 04:29 PM

Was This Post Helpful? 1
  • +
  • -

#5 huma samrin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-April 08

Re: REMOVE leading Spaces in String Array

Posted 07 May 2010 - 10:09 PM

TAanks a lot
Its solve my problem :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1