已知:
string str = "1,2,3,4,5"
问:
如何根据上述字符串产生一个int[]数组?(int[] intLst = {1,2,3,4,5})
答:
首先:string strLst = str.Split( ',' )
然后:int[] intLst = Array.ConvertAll( strLst , int.Parse )
整理自VS
Split()方法介绍:
// 摘要:
// Returns a string array that contains the substrings in this instance that are
// delimited by elements of a specified Unicode character array.
//
// 参数:
// separator:
// An array of Unicode characters that delimit the substrings in this instance,
// an empty array that contains no delimiters, or null.
//
// 返回结果:
// An array whose elements contain the substrings in this instance that are delimited
// by one or more characters in separator. For more information, see the Remarks
// section.
public String[] Split(params char[] separator);
ConvertAll()方法介绍:
// 摘要:
// Converts an array of one type to an array of another type.
//
// 参数:
// array:
// The one-dimensional, zero-based System.Array to convert to a target type.
//
// converter:
// A System.Converter`2 that converts each element from one type to another type.
//
// 类型参数:
// TInput:
// The type of the elements of the source array.
//
// TOutput:
// The type of the elements of the target array.
//
// 返回结果:
// An array of the target type containing the converted elements from the source
// array.
//
// 异常:
// T:System.ArgumentNullException:
// array is null.-or-converter is null.
public static TOutput[] ConvertAll<TInput, TOutput>(TInput[] array, Converter<TInput, TOutput> converter);
int.Parse()方法介绍:
// 摘要:
// Converts the string representation of a number in a specified style and culture-specific
// format to its 32-bit signed integer equivalent.
//
// 参数:
// s:
// A string containing a number to convert.
//
// style:
// A bitwise combination of enumeration values that indicates the style elements
// that can be present in s. A typical value to specify is System.Globalization.NumberStyles.Integer.
//
// provider:
// An object that supplies culture-specific information about the format of s.
//
// 返回结果:
// A 32-bit signed integer equivalent to the number specified in s.
//
// 异常:
// T:System.ArgumentNullException:
// s is null.
//
// T:System.ArgumentException:
// style is not a System.Globalization.NumberStyles value. -or-style is not a combination
// of System.Globalization.NumberStyles.AllowHexSpecifier and System.Globalization.NumberStyles.HexNumber
// values.
//
// T:System.FormatException:
// s is not in a format compliant with style.
//
// T:System.OverflowException:
// s represents a number less than System.Int32.MinValue or greater than System.Int32.MaxValue.
// -or-s includes non-zero, fractional digits.
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public static Int32 Parse(string s, NumberStyles style, IFormatProvider provider);
// 摘要:
// Converts the string representation of a number in a specified culture-specific
// format to its 32-bit signed integer equivalent.
//
// 参数:
// s:
// A string containing a number to convert.
//
// provider:
// An object that supplies culture-specific formatting information about s.
//
// 返回结果:
// A 32-bit signed integer equivalent to the number specified in s.
//
// 异常:
// T:System.ArgumentNullException:
// s is null.
//
// T:System.FormatException:
// s is not of the correct format.
//
// T:System.OverflowException:
// s represents a number less than System.Int32.MinValue or greater than System.Int32.MaxValue.
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public static Int32 Parse(string s, IFormatProvider provider);
// 摘要:
// Converts the string representation of a number to its 32-bit signed integer equivalent.
//
// 参数:
// s:
// A string containing a number to convert.
//
// 返回结果:
// A 32-bit signed integer equivalent to the number contained in s.
//
// 异常:
// T:System.ArgumentNullException:
// s is null.
//
// T:System.FormatException:
// s is not in the correct format.
//
// T:System.OverflowException:
// s represents a number less than System.Int32.MinValue or greater than System.Int32.MaxValue.
public static Int32 Parse(string s);
// 摘要:
// Converts the string representation of a number in a specified style to its 32-bit
// signed integer equivalent.
//
// 参数:
// s:
// A string containing a number to convert.
//
// style:
// A bitwise combination of the enumeration values that indicates the style elements
// that can be present in s. A typical value to specify is System.Globalization.NumberStyles.Integer.
//
// 返回结果:
// A 32-bit signed integer equivalent to the number specified in s.
//
// 异常:
// T:System.ArgumentNullException:
// s is null.
//
// T:System.ArgumentException:
// style is not a System.Globalization.NumberStyles value. -or-style is not a combination
// of System.Globalization.NumberStyles.AllowHexSpecifier and System.Globalization.NumberStyles.HexNumber
// values.
//
// T:System.FormatException:
// s is not in a format compliant with style.
//
// T:System.OverflowException:
// s represents a number less than System.Int32.MinValue or greater than System.Int32.MaxValue.
// -or-s includes non-zero, fractional digits.
public static Int32 Parse(string s, NumberStyles style);