Safely splits the string and trims spaces from elements. - CSharp System

CSharp examples for System:String Split

Description

Safely splits the string and trims spaces from elements.

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using System;//from   w  w w.  ja va2s  .  co m

public class Main{
        /// <summary>Safely splits the string and trims spaces from elements.</summary>
    /// <param name="value"></param>
    /// <param name="separators"></param>
    /// <returns></returns>
    public static string[] SplitNames(this string value, params char[] separators) {
      if (string.IsNullOrWhiteSpace(value))
        return new string[] { };
      if (separators == null || separators.Length == 0)
        separators = new char[] { ',', ';' };
      return value.Split(separators).Select(s => s.Trim()).ToArray();
    }
}

Related Tutorials