字符串分割(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 ...
随机推荐
- node: #!/usr/bin/env node
声明 windows中不支持Shebang,它是通过文件的扩展名来确定使用什么解释器来执行脚本 参考链接: https://juejin.cn/post/6844903826344902670
- 【译】为你的 ASP. NET Core Web API 创建 Microsoft Power App
通过轻松创建 Web API 前端来提升您的开发体验. 低代码工具在开发人员中越来越流行,因为用更少的代码更快地创建应用程序.在 Visual Studio 2022 17.6 预览版2中,您现在可以 ...
- 下一代MES系统架构分析与选型参考
本文分享自华为云社区<工业互联网系列(十一):下一代MES系统架构分析与选型参考>,作者:云起MAE . 目前国内制造执行系统MES市场尚处于"功能机"混战年代,市场集 ...
- 一种创新的 Hybird App 技术开发模式
Hybrid这个词,在App开发领域,相信大家都不陌生.Hybrid App是指介于web-app.native-app这两者之间的app,它虽然看上去是一个Native App,但只有一个UI We ...
- 【Azure App Service】为部署在App Service上的PHP应用开启JIT编译器
问题描述 在App Service for linux上创建一个PHP应用,通过 phpinfo() 查看PHP的扩展设置,发现JIT没有被开启, jit_buffer_size 大小为0. 那么,在 ...
- 微服务集成redis并通过redis实现排行榜的功能
默认你已经看过我之前的教程了,并且拥有上个教程完成的项目, 之前的教程 https://www.cnblogs.com/leafstar/p/17638933.html 由于redis的安装网上教程很 ...
- PDF 补丁丁 1.0 正式版
经过了一年多的测试和完善,PDF 补丁丁发布第一个开放源代码的正式版本了. PDF 补丁丁也是国内首先开放源代码.带有修改和阅读PDF的功能的 PDF 处理程序之一. 源代码网址:https://gi ...
- SQL注入——搜索型
SQL注入-搜索型 搜索型注入-原理介绍 一些网站为了方便用户查找网站的资源,都对用户提供了搜索的功能,因为是搜索功能,往往是程序员在编写代码时都忽略了对其变量(参数)的过滤,而且这样的漏洞在国内的系 ...
- @Async注解详解 以及 可能遇到的各种问题
一.简介1)在方法上使用该@Async注解,申明该方法是一个异步任务:2)在类上面使用该@Async注解,申明该类中的所有方法都是异步任务:3)方法上一旦标记了这个@Async注解,当其它线程调用这个 ...
- idea如何显示出分支名
如图所示 配置修改 idea安装目录下bin/idea.properties文件,新增2行配置 project.tree.structure.show.url=false ide.tree.horiz ...