如何将字符串转换为字符串[]? [英] How can a string to converted to a string[]?

查看:136
本文介绍了如何将字符串转换为字符串[]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于这样的字符串值:

For such a string value:

string s = "[\"A01B001\",\"BM OB-A-1\",\"A01\",\"BENCHMARK\",\"PRIVATE\",\"BRASS DISK\"]";



如何将其转换为串[]?谢谢你的帮助。



我的尝试:



如何可以将字符串转换为字符串[]?


How can it be converted to a string[]? Thanks for your help.

What I have tried:

How can a string to converted to a string[]?

推荐答案

试试这个

方法之一,

try this
one of the method,
string s = "[\"A01B001\",\"BM OB-A-1\",\"A01\",\"BENCHMARK\",\"PRIVATE\",\"BRASS DISK\"]";
       string[] result = s.Trim(new char[] { '[', ']' }).Split(',').Select(k => k.Trim('"')).ToArray();


我猜你想分开它用逗号分隔的项目,简单:

I'm just guessing you want to split it into the items which are separated by commas, and that bits simple:
string[] parts = s.Split(',');



但是如果你还想转储方括号和双引号,那就更多了:


But if you also want to dump the square brackets and the double quotes, that's a little more work:

string s = "[\"A01B001\",\"BM OB-A-1\",\"A01\",\"BENCHMARK\",\"PRIVATE\",\"BRASS DISK\"]";
string[] parts = s.Split("\"[],".ToArray(), StringSplitOptions.RemoveEmptyEntries);

将会这样做。


In解决方案1和3的补充。

当你想进行模式匹配时,使用的工具是正则表达式。

基本上,当你要检查那个时使用RegEx字符串匹配或包含给定格式,如日期,浮点数,整数,电话号码......

一种用法也是将一个字符串拆分。



以下是RegEx文档的链接:

perlre - perldoc.perl.org [ ^ ]

以下是帮助构建RegEx并调试它们的工具的链接:

.NET正则表达式测试程序 - 正则表达式风暴 [<一个href =http: //regexstorm.net/testertarget =_ blanktitle =新窗口> ^ ]

Expresso正则表达式工具 [ ^ ]

这个显示RegEx是一个很好的图表,它非常有助于理解RegEx的作用:

Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]
In addition to Solutions 1 and 3.
When you want to do pattern matching, the tool to use is Regular Expressions.
Basically, RegEx is used when you want to check that a string match or contain a given format like date, floating point number, integer, phone number ...
One usage is also to split a string in pieces.

Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]


这篇关于如何将字符串转换为字符串[]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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