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

我这里写了个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. Lora简介

    断断续续接触lora已经有几年时间了,一直用lora来做点对点的传输,近来有朋友想通过Lora来做广播群发和群收管理,想通过低成本方式实现,sx1302几百的银子,成本有点高,尝试通过sx1278/L ...

  2. MISC杂项解题思路

    首先拿到一个杂项的附件 第一步要判断 是什么类型的杂项题目 附件是什么内容 是图片? 是压缩包? 是磁盘文件? 还是其他未知的东西 第一步的判断能够直接将解题思路精准定位到正确的区域下 加快解题速度 ...

  3. 在webpack中这样分离环境和代码就好啦

    前面的文章中,webpack.config.js 中包含本地调试和线上发布的所有配置,编译后的 bundle.js 包含所有的代码. 当项目变大.代码量变多.配置增加的时候,文件的可维护性会越来越差, ...

  4. quarkus依赖注入之九:bean读写锁

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本篇是<quarkus依赖注入> ...

  5. 一文搞明白STM32芯片存储结构

    一.前言 本篇介绍STM32芯片的存储结构,ARM公司负责提供设计内核,而其他外设则为芯片商设计并使用,ARM收取其专利费用而不参与其他经济活动,半导体芯片厂商拿到内核授权后,根据产品需求,添加各类组 ...

  6. BUUCTF Reverse-[FlareOn6]Overlong-动态调试

    没有什么问题,直接进 三个函数,字符串也没啥特殊的 应该是个加密 返回上面分析 数据很大,你忍一下 也就是说它会找28位加密 然后我们接着分析 这个提示刚好28位 也就是说28位对应这个框 如果我们修 ...

  7. linux 查找目录中的大文件

    find是Linux系统中常用的文件查找命令.它可以在文件系统中查找指定条件的文件,并执行相应的操作.语法格式如下: find [pathname] [options] pathname: 指定查找的 ...

  8. (2023.7.24)软件加密与解密-2-1-程序分析方法[XDbg].md

    body { font-size: 15px; color: rgba(51, 51, 51, 1); background: rgba(255, 255, 255, 1); font-family: ...

  9. 代替forever下一个部署node的持久化工具---pm2

    最近有个后端项目,用的是node,在持久化的时候会挂掉,详细了解到用的是nohup,然后先详细了解了一下nohup nohup是一个Linux命令,用于在系统后台不挂断地运行命令,退出终端不会影响程序 ...

  10. Python操作Redis大全

    一.字符串 string Python操作Redis的redis模块对字符串(string)的主要操作函数包括:SET.GET.GETSET.SETEX.SETNX.MSET.MSETNX.INCR( ...