在多个定界符上分隔字符串时,我怎么知道使用了哪个定界符?(C#) [英] How do I know which delimiter was used when delimiting a string on multiple delimiters? (C#)

查看:68
本文介绍了在多个定界符上分隔字符串时,我怎么知道使用了哪个定界符?(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从文件中读取字符串,它们的样式多种多样:

I read strings from a file and they come in various styles:

item0 item1 item2 
item0,item1,item2
item0_item1_item2

我这样分割他们:

string[] split_line = line[i].split(new char[] {' ',',','_'});

我更改一个项目(列),然后使用字符串生成器将字符串缝合在一起.但是现在当放回字符串时,我必须使用正确的定界符.

I change an item (column) and then i stitch the strings back together using string builder. But now when putting the string back I have to use the right delimiter.

是否可以知道在分割字符串时使用了哪个定界符?

Is it possible to know which delimiter was used when splitting the string?

更新 呼叫者将把第一项传递给我,以便我仅更改该行.

UPDATE the caller will pass me the first item so that I only change that line.

推荐答案

除非您跟踪拆分操作(一次不进行),否则不要这样做.否则,您可以创建一个正则表达式,以捕获项目和定界符并从那里开始.

Unless you keep track of splitting action (one at the time) you don't. Otherwise, you could create a regular expression, to catch the item and the delimiter and go from there.

这篇关于在多个定界符上分隔字符串时,我怎么知道使用了哪个定界符?(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆