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[0], val[1], val[2], val[3]);
    result = String.Join(sep, val, 1, 2);
    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", "", "1", "2", "", "Yuri" };
        //目标状态:{李***,李***,李***,单**,单**,单*,单*,单,1,2,罗,Y***};
        string[] find = { "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*" };
        for (int i = 0; i < names.Count; i++)
        {
            string temp = names[i][0] + string.Join("", find, 0, names[i].Length-1);
            Console.WriteLine(temp);
        }

        List<string> phone = new List<string> { "152", "15347834899", "1", "8967382" };

        for (int i = 0; i < phone.Count; i++)
        {
            if (phone[i].Length>7)
            {
                string temp = phone[i].Substring(0, 3) + "****" + phone[i].Substring(7);
                Console.WriteLine(temp);    
            }
            else if (phone[i].Length>3)
            {
                string temp = phone[i].Substring(0, 3) +string.Join("",find,0, phone[i].Length-3);
                Console.WriteLine(temp);
            }
            else
            {
                Console.WriteLine(phone[i]);
            }
        }

        Console.ReadKey();
测试

原文地址:https://www.cnblogs.com/danlis/p/5347437.html