今天有个群友问了这个问题:"字符串分割时,如何连同分隔符一起返回?"。

我这里写了个String扩展类,模仿原生的Split方法,与原生Split方法的区别在于多了个返回分隔符的枚举功能。

class Program
{
static void Main(string[] args)
{
var flagEx = StringSplitOptionsEx.RemoveEmptyEntries | StringSplitOptionsEx.ReturnSepapator | StringSplitOptionsEx.TrimEntries;
var result = " A | B | |C|D".Split("|", flagEx);
Console.WriteLine(result);
}
} [Flags]
public enum StringSplitOptionsEx
{
None = 0,
RemoveEmptyEntries = 1,
TrimEntries = 2,
ReturnSepapator = 4
} public static class StringExtensions
{
private readonly static string[] StringSplitOptionsNames = Enum.GetNames<StringSplitOptions>(); public static string[] Split(this string str, char separator, StringSplitOptionsEx optionsEx = StringSplitOptionsEx.None)
{
return str.Split(separator.ToString(), optionsEx);
} public static string[] Split(this string source, string separator, StringSplitOptionsEx optionsEx = StringSplitOptionsEx.None)
{
if (!optionsEx.HasFlag(StringSplitOptionsEx.ReturnSepapator))
{
return source.Split(separator, (StringSplitOptions)optionsEx);
} string[] optionsExNames = optionsEx.ToString().Split(',', StringSplitOptions.TrimEntries);
StringSplitOptions options = optionsExNames.Intersect(StringSplitOptionsNames).Select(t => Enum.Parse<StringSplitOptions>(t)).Aggregate((a, b) => { return a | b; });
string[] result = source.Split(separator, options);
if (!optionsEx.HasFlag(StringSplitOptionsEx.ReturnSepapator))
{
return result;
} string[] newResult = new string[result.Length * 2 - 1];
for (int i = 0; i < result.Length; i++)
{
newResult[i * 2] = result[i];
}
for (int i = 0; i < result.Length - 1; i++)
{
newResult[i * 2 + 1] = separator;
}
return newResult;
} public static string[] Split(this string source, char[] separators, StringSplitOptionsEx optionsEx = StringSplitOptionsEx.None)
{
if (!optionsEx.HasFlag(StringSplitOptionsEx.ReturnSepapator))
{
return source.Split(separators, (StringSplitOptions)optionsEx);
} if (optionsEx.HasFlag(StringSplitOptionsEx.RemoveEmptyEntries))
{
throw new ArgumentException($"{nameof(StringSplitOptionsEx.RemoveEmptyEntries)} and {StringSplitOptionsEx.ReturnSepapator} cannot be used in combination", nameof(optionsEx));
} string[] optionsExNames = optionsEx.ToString().Split(',', StringSplitOptions.TrimEntries);
StringSplitOptions options = optionsExNames.Intersect(StringSplitOptionsNames).Select(t => Enum.Parse<StringSplitOptions>(t)).Aggregate((a, b) => { return a | b; });
string[] result = source.Split(separators, options); char[] separatorValues = new char[result.Length - 1];
int foundCount = 0;
for (int i = 0; i < source.Length; i++)
{
for (int j = 0; j < separators.Length; j++)
{
if (source[i] == separators[j])
{
separatorValues[foundCount] = separators[j];
foundCount++;
break;
}
}
} string[] newResult = new string[result.Length * 2 - 1];
for (int i = 0; i < result.Length; i++)
{
newResult[i * 2] = result[i];
}
for (int i = 0; i < result.Length - 1; i++)
{
newResult[i * 2 + 1] = separatorValues[i].ToString();
}
return newResult;
}
}
output1:
A
|
B
| |
C
|
D
\
EEEE output2:
A
|
B
|
C
|
D

字符串分割(String.Split)时连同分隔符一起返回的更多相关文章

  1. Java字符串分割函数split源码分析

    spilt方法作用 以所有匹配regex的子串为分隔符,将input划分为多个子串. 例如: The input "boo:and:foo", for example, yield ...

  2. 用Matlab实现字符串分割(split)

    用Matlab实现字符串分割(split)Posted on 2011/08/08 Matlab的字符串处理没有C#强大,本身又没有提供OO特性,需要依赖别的手段完成这项任务. 我们在这里借助正则表达 ...

  3. JavaScript中字符串分割函数split用法实例

    这篇文章主要介绍了JavaScript中字符串分割函数split用法,实例分析了javascript中split函数操作字符串的技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了JavaSc ...

  4. (转)C++常见问题: 字符串分割函数 split

    http://www.cnblogs.com/dfcao/p/cpp-FAQ-split.html C++标准库里面没有字符分割函数split ,这可太不方便了,我已经遇到>3次如何对字符串快速 ...

  5. C++常见问题: 字符串分割函数 split

    C++标准库里面没有字符分割函数split ,这可太不方便了,我已经遇到>3次如何对字符串快速分割这个问题了.列几个常用方法以备不时之需. 方法一: 利用STL自己实现split 函数(常用,简 ...

  6. Delphi 自带的字符串分割函数split

    下面介绍Delphi自带的字符串分割函数,根据你的需要来使用. 1.ExtractStrings function ExtractStrings(Separators, WhiteSpace: TSy ...

  7. 字符串切分 String.Split 和 Regex.Split

    当切割字符串的是单个字符时可使用String.Split string strSample="ProductID:20150215,Categroy:Food,Price:15.00&quo ...

  8. C++之字符串分割函数split

    c++之字符串分割: /* *c++之字符串分割: */ #include <iostream> #include <string> #include <vector&g ...

  9. 字符串切分 String.Split 和 Regex.Split(小技巧)

    当切割字符串的是单个字符时可使用String.Split string strSample="ProductID:20150215,Categroy:Food,Price:15.00&quo ...

  10. SQL Server自定义字符串分割函数——Split

    我相信大部分人都碰到过,处理数据的时候,字段的值是以 ',' (逗号)分隔的形式,所以我也不能避免. 然后我才知道,sql 是没有类似于 C# 和 Javascript 这种分割字符串的方法.( Sp ...

随机推荐

  1. Angular: Error: NG0100: ExpressionChangedAfterItHasBeenChecked

    错误原因 当变更检测完成后又更改了表达式的值时,Angular就会抛出ExpressionChangedAfterItHasBeenCheckedError 错误,Angular只会在开发模式下抛出此 ...

  2. docker网络 bridge 与overlay 模式

    转载请注明出处: 1.bridge网络模式 工作原理:  在Bridge模式中,Docker通过创建一个虚拟网络桥接器(bridge)将容器连接到主机上的物理网络接口.每个容器都会被分配一个IP地址, ...

  3. SpringBoot3集成Redis

    目录 一.简介 二.工程搭建 1.工程结构 2.依赖管理 3.Redis配置 三.Redis用法 1.环境搭建 2.数据类型 3.加锁机制 四.Mybatis缓存 1.基础配置 2.自定义实现 五.参 ...

  4. vue中使用Tinymce

    1.安装tinymce编辑器 npm i tinymcenpm i @tinymce/tinymce-vue 或: yarn add tinymce yarn add @tinymce/tinymce ...

  5. 《Kali渗透基础》14. 无线渗透(四)

    @ 目录 1:相关工具 1.1:Aircrack-ng 1.1.1:airmon-ng 1.1.2:airodump-ng 1.1.3:aireplay-ng 1.1.4:airolib-ng 1.1 ...

  6. 【Python进阶-PyQt5】00PyQt5简介

    0.图形用户界面-开发选择 在Python基础的教程中,我们程序的用户交互界面都是运行窗口.这个运行窗口对于我们编程者来说直观明了,但是对于一些相对复杂的程序,用户使用上就会变得十分麻烦.所以,我们要 ...

  7. 小白快速入门SRC挖掘(以edusrc为例)

    edusrc平台介绍 我们可以在关于页面看到edusrc的收录规则: 现阶段,教育行业漏洞报告平台接收如下类别单位漏洞: 教育部 各省.自治区教育厅.直辖市教委.各级教育局 学校 教育相关软件 可以看 ...

  8. 面试官:说一下 MyBatis 缓存机制?

    MyBatis 的缓存机制属于本地缓存,适用于单机系统,它的作用是减少数据库的查询次数,提高系统性能. MyBaits 中包含两级本地缓存: 一级缓存:SqlSession 级别的,是 MyBatis ...

  9. 使用 Python ssh 远程登陆服务器的最佳方案

    在使用 Python 写一些脚本的时候,在某些情况下,我们需要频繁登陆远程服务去执行一次命令,并返回一些结果. 在 shell 环境中,我们是这样子做的. sshpass -p ${passwd} s ...

  10. Electron包装网站的问题

    原文链接 原文链接 Preface 最近尝试了很多不错的在线工具,只是每次都要进入网站,有点麻烦,于是想到之前了解过的electron,尝试一下打包成本地应用. Contents 1.下载所有源文件 ...