using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OverWrite_Split
{
class Program
{
public static string[] MyStringSplit(string input, char[] separator)
{
string[] output = null;
//获得切割多少段
if (input != null && separator != null)
{
int num = 1;
for (int i = 0; i < input.Length; i++)
{
for (int j = 0; j < separator.Length; j++)
{
if (input[i] == separator[j])
{
num++;
break;
}
}
}
//为每一段赋值 连续的字符 不满足切割点
output = new string[num];
int index = 0;
bool isSeparator = false,oneTime = false;
for (int i = 0; i < input.Length; i++)
{
for (int j = 0; j < separator.Length; j++)
{
if (input[i] == separator[j])
{
index++;
isSeparator = true;
break;
}
else {
//是否连续
isSeparator = false;
}
}
//Console.WriteLine("!isSeparator"+ !isSeparator);
if (!isSeparator)
{
output[index] += input[i].ToString();//处理连续字符
}
}
}
return output;
}
static void Main(string[] args)
{
//Console.WriteLine("--"+' '+'\u0000'+'-'+' ');
//string test = null;
//string test = string.Empty;
string test = ".as f.,ad_sf..";
char[] separators = new char[] { '.', ',', '_', ' ' };
string[] a;
Console.WriteLine("官方-------------------------------");
a = test.Split(separators);
Console.WriteLine(a.Length);
foreach (string item in a)
{
Console.WriteLine(item);
}
Console.WriteLine("重写-------------------------------");
a = MyStringSplit(test, separators);
Console.WriteLine(a.Length);
foreach (string item in a)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
C#重写分隔符分割字符串 --Split
最新推荐文章于 2023-03-22 18:09:49 发布