String.Join重载String.Join 方法 (String, String[], Int32, Int32)

时间:2023-03-09 06:04:34
String.Join重载String.Join 方法 (String, String[], Int32, Int32)

https://msdn.microsoft.com/zh-cn/library/tk0xe5h0

String.Join 方法 (String, String[], Int32, Int32)

官方样例

串联字符串数组的指定元素,其中在每个元素之间使用指定的分隔符。

命名空间:   System
程序集:
 mscorlib(mscorlib.dll 中)

public static string Join(
string separator,
string[] value,
int startIndex,
int count
)
        // 摘要:
        //     串联字符串数组的指定元素,其中在每个元素之间使用指定的分隔符。
        //
        // 参数:
        //   separator:
        //     要用作分隔符的字符串。
        //
        //   value:
        //     一个数组,其中包含要连接的元素。
        //
        //   startIndex:
        //     value 中要使用的第一个元素。
        //
        //   count:
        //     要使用的 value 的元素数。
        //
        // 返回结果:
        //     由 value 中的字符串组成的字符串,这些字符串以 separator 字符串分隔。- 或 -如果 count 为零,value 没有元素,或
        //     separator 以及 value 的全部元素均为 System.String.Empty,则为 System.String.Empty。
        //
        // 异常:
        //   System.ArgumentNullException:
        //     value 为 null。
        //
        //   System.ArgumentOutOfRangeException:
        //     startIndex 或 count 小于 0。- 或 -startIndex 加上 count 大于 value 中的元素数。
        //
        //   System.OutOfMemoryException:
        //     内存不足。
// Sample for String.Join(String, String[], int int)
using System; class Sample {
public static void Main() {
String[] val = {"apple", "orange", "grape", "pear"};
String sep = ", ";
String result; Console.WriteLine("sep = '{0}'", sep);
Console.WriteLine("val[] = {{'{0}' '{1}' '{2}' '{3}'}}", val[], val[], val[], val[]);
result = String.Join(sep, val, , );
Console.WriteLine("String.Join(sep, val, 1, 2) = '{0}'", result);
}
}
/*
This example produces the following results:
sep = ', '
val[] = {'apple' 'orange' 'grape' 'pear'}
String.Join(sep, val, 1, 2) = 'orange, grape'
*/

Demo

Console.WriteLine("************************************************************");

        List<string> names = new List<string> { "李意义1", "李意义2", "李礼物1", "李礼物2", "单罗1", "单罗2", "单1", "单2", "单", "", "", "罗", "Yuri" };
//目标状态:{李***,李***,李***,单**,单**,单*,单*,单,1,2,罗,Y***};
string[] find = { "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*" };
for (int i = ; i < names.Count; i++)
{
string temp = names[i][] + string.Join("", find, , names[i].Length-);
Console.WriteLine(temp);
} List<string> phone = new List<string> { "", "", "", "" }; for (int i = ; i < phone.Count; i++)
{
if (phone[i].Length>)
{
string temp = phone[i].Substring(, ) + "****" + phone[i].Substring();
Console.WriteLine(temp);
}
else if (phone[i].Length>)
{
string temp = phone[i].Substring(, ) +string.Join("",find,, phone[i].Length-);
Console.WriteLine(temp);
}
else
{
Console.WriteLine(phone[i]);
}
} Console.ReadKey();

测试

String.Join重载String.Join 方法 (String, String[], Int32, Int32)