字符串分割(String.Split)时连同分隔符一起返回
今天有个群友问了这个问题:"字符串分割时,如何连同分隔符一起返回?"。
我这里写了个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)时连同分隔符一起返回的更多相关文章
- Java字符串分割函数split源码分析
spilt方法作用 以所有匹配regex的子串为分隔符,将input划分为多个子串. 例如: The input "boo:and:foo", for example, yield ...
- 用Matlab实现字符串分割(split)
用Matlab实现字符串分割(split)Posted on 2011/08/08 Matlab的字符串处理没有C#强大,本身又没有提供OO特性,需要依赖别的手段完成这项任务. 我们在这里借助正则表达 ...
- JavaScript中字符串分割函数split用法实例
这篇文章主要介绍了JavaScript中字符串分割函数split用法,实例分析了javascript中split函数操作字符串的技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了JavaSc ...
- (转)C++常见问题: 字符串分割函数 split
http://www.cnblogs.com/dfcao/p/cpp-FAQ-split.html C++标准库里面没有字符分割函数split ,这可太不方便了,我已经遇到>3次如何对字符串快速 ...
- C++常见问题: 字符串分割函数 split
C++标准库里面没有字符分割函数split ,这可太不方便了,我已经遇到>3次如何对字符串快速分割这个问题了.列几个常用方法以备不时之需. 方法一: 利用STL自己实现split 函数(常用,简 ...
- Delphi 自带的字符串分割函数split
下面介绍Delphi自带的字符串分割函数,根据你的需要来使用. 1.ExtractStrings function ExtractStrings(Separators, WhiteSpace: TSy ...
- 字符串切分 String.Split 和 Regex.Split
当切割字符串的是单个字符时可使用String.Split string strSample="ProductID:20150215,Categroy:Food,Price:15.00&quo ...
- C++之字符串分割函数split
c++之字符串分割: /* *c++之字符串分割: */ #include <iostream> #include <string> #include <vector&g ...
- 字符串切分 String.Split 和 Regex.Split(小技巧)
当切割字符串的是单个字符时可使用String.Split string strSample="ProductID:20150215,Categroy:Food,Price:15.00&quo ...
- SQL Server自定义字符串分割函数——Split
我相信大部分人都碰到过,处理数据的时候,字段的值是以 ',' (逗号)分隔的形式,所以我也不能避免. 然后我才知道,sql 是没有类似于 C# 和 Javascript 这种分割字符串的方法.( Sp ...
随机推荐
- Promise的理解与使用(一)
一.Promise是什么?Promise是JS中进行异步操作的新的解决方案(旧的方案是回调函数的形式,回调函数里嵌套函数)从语法上来说,Promise是一个构造函数.从功能上来说,用Promise的实 ...
- HTTP请求时哪些请求头是默认携带的?
提起HTTP的请求头和响应头总是一头雾水,因为总是看上去一大堆,好多还不知道是什么意思. 今天我们先研究请求头,我就想,如果我们能弄清楚,在我们什么都不做的情况下,一个最简单的HTTP请求会携带哪些请 ...
- 【译】通过 GitHub Copilot Chat 简化代码优化和调试(AI 辅助编程)
今年3月,我们宣布了 Visual Studio 2022 的 GitHub Copilot Chat.通过 Chat, Copilot 已经超越了代码补全,提供了对代码工作原理的深入分析和解释.它支 ...
- SQL取上一条, 下一条记录方法
如果我们需要取id为3的前后的1条记录. 就可以用以下方法 取上一条记录: select * from `表名` where `id`<3 order by `id` desc limit ...
- .NET中测量多线程基准性能
多线程基准性能是用来衡量计算机系统或应用程序在多线程环境下的执行能力和性能的度量指标.它通常用来评估系统在并行处理任务时的效率和性能.测量中通常创建多个线程并在这些线程上执行并发任务,以模拟实际应用程 ...
- SpringBoot + Redis + Token 解决接口幂等性问题
前言 SpringBoot实现接口幂等性的方案有很多,其中最常用的一种就是 token + redis 方式来实现. 下面我就通过一个案例代码,帮大家理解这种实现逻辑. 原理 前端获取服务端getTo ...
- 有人说SaToken吃相难看,你怎么看。
前言 今天摸鱼逛知乎,偶然看到了一个回答,8月份的,是关于SaToken的,一时好奇就点了进去. 好家伙,因为一个star的问题,提问的人抱怨了许多,我有些意外,就仔细看了下面的评论,想知道一部分人的 ...
- 解决Dependency 'fastdfs-client-java’not found
如何能把 fastdfs的jar包安装到本地的仓库中(因为中央仓库没有FASTDFS的jar包地址) 1.首先去github上下载下来fastdfs的压缩包 下载链接 然后直接解压出来 2.使用cmd ...
- Go 复合类型之切片类型介绍
Go 复合类型之切片类型 目录 Go 复合类型之切片类型 一.引入 二.切片(Slice)概述 2.1 基本介绍 2.2 特点 2.3 切片与数组的区别 三. 切片声明与初始化 3.1 方式一:使用切 ...
- openssl加解密实战
一.概念 1.加密有两种类型 密钥加密(secret-key encryption)使用相同的密钥进行加密和解密,也叫对称加密 公钥加密(public-key encryption)使用不同的密钥进行 ...