字符串分割(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 ...
随机推荐
- 天地图三维帮助文档(Cesium)
https://blog.csdn.net/Tmraz/article/details/114977652
- quarkus依赖注入之七:生命周期回调
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本篇的知识点是bean的生命周期回调:在be ...
- [kvm]创建虚拟机
创建虚拟机示例 # 使用iso创建虚拟机 virt-install --virt-type kvm --os-type=linux --name temp_debian11 \ --memory 16 ...
- 创建python虚拟环境并打包python文件
前言 当需要为一个离线环境部署python应用时,离线环境可能缺少各种python环境,有docker的话可以用docker,没有docker可以用pyinstaller打包成二进制文件.pyinst ...
- 小版本更新kubernetes
小版本更新kubernetes 背景 最近一段时间躺平了没有更新我的博客文档.感谢各位小伙伴一直以来的支持. 此脚本基于 https://github.com/cby-chen/Kubernetes/ ...
- SAP 传输请求释放及传输过程 SE10 STMS
T-CODE:SE10 STMS 1.传输请求释放 首先通过SE10打开传输组织器. 点击[显示],可以看到待释放的请求. 此时将可修改请求中的请求,点击进行展开,可以看到子请求号和请求属性. 选中请 ...
- LeetCode 周赛上分之旅 #39 结合中心扩展的单调栈贪心问题
️ 本文已收录到 AndroidFamily,技术和职场问题,请关注公众号 [彭旭锐] 和 BaguTree Pro 知识星球提问. 学习数据结构与算法的关键在于掌握问题背后的算法思维框架,你的思考越 ...
- [ABC305D] Sleep Log题解
题目大意 给 \(N\) 个时刻: 当 \(i\) 为奇数时,\(A_i\) 表示刚刚起床的时刻. 当 \(i\) 为偶数时,\(A_i\) 表示开始睡觉的时刻. 有 \(Q\) 次询问,每次求在 \ ...
- Vue【原创】时间轴 【time-axis】&【date-axis】
封装了关于时间轴的组件,有时候统计页面会用到. 效果图: 时间轴分为2种,一种是time-axis:范围选择模式,一种是date-axis:步长选择模式. 代码中涉及到的工具类和图片资源,请移步页面底 ...
- 淘宝详情api接口的应用
淘宝详情API接口是一个基于HTTP协议的接口服务,可用于获取淘宝商品的具体信息.下面将介绍如何调用淘宝详情API接口获取淘宝商品数据的步骤. 1.注册账号并创建应用 首先,我们需要进行账号注册.实名 ...