如何使用字符串分隔符拆分字符串? [英] How can I split a string with a string delimiter?

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

问题描述

我有这个字符串:

My name is Marco and I'm from Italy

我想分割它,用分隔符是Marco和,所以我应该得到一个带有

I'd like to split it, with delimiter is Marco and, so I should get an array with

  • 我的名字在[0]和
  • 我来自意大利在 [1].
  • My name at [0] and
  • I'm from Italy at [1].

我如何用 C# 做到这一点?

How can I do it with C#?

我尝试过:

.Split("is Marco and")

但它只需要一个字符.

推荐答案

string[] tokens = str.Split(new[] { "is Marco and" }, StringSplitOptions.None);

如果您有一个单字符分隔符(例如 ,),您可以将其缩减为(注意单引号):

If you have a single character delimiter (like for instance ,), you can reduce that to (note the single quotes):

string[] tokens = str.Split(',');

这篇关于如何使用字符串分隔符拆分字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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