Test code:
string files = "C:\Hello; C:\Hi; D:\Goodmorning; D:\Goodafternoon; E:\Goodevening";
string[] paths = files.Split(';');
foreach (string s in paths)
{
    MessageBox.Show(s);
}
How do I remove the whitespaces before storing it into the array?
You can use String.Trim method, like this:
foreach (string s in paths)
{
    MessageBox.Show(s.Trim());
}
Alternatively, you can eliminate spaces before they enter paths, like this:
files.Split(new[]{';', ' '}, StringSplitOptions.RemoveEmptyEntries);
                        .NET 2.0
string[] paths = Array.ConvertAll(files.Split(';'), a => a.Trim());
.NET 3.5
string[] paths = files.Split(';').Select(a => a.Trim()).ToArray();
                        Nevermind, I solved it..
My code:
string files = "C:\Hello; C:\Hi; D:\Goodmorning; D:\Goodafternoon; E:\Goodevening";
string[] paths = files.Trim().Split(';');
List<string> cleanPath = new List<string>();
 int x = 0;
 foreach (string s in paths)
 {
     cleanPath.Add(s.Trim());
 }
 foreach(string viewList in cleanPath)
 {
      x++;
      MessageBox.Show(x + ".)" +viewList);//I put x.) just to know whether it still has whitespace characters.
 }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With