如何将字符串拆分为两个子字符串 [英] How to split a string to two sub strings

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

问题描述

我有一个字符串

string source =当前状态:这是测试当前状态。用户体验:这是测试用户体验



我想将此字符串拆分为两个子字符串



string stra =当前状态:这是测试当前状态。

string strb =用户体验:这是测试用户体验



i尝试过这些方法



string [] stringSeparators = new string [] {User Experience};

var result1 = source.Split(stringSeparators,StringSplitOptions.None);



var rx = new System.Text.RegularExpressions.Regex(用户体验);

var array = rx.Split(source);



但我得到的结果如



string stra =当前状态:这是测试当前状态。

string strb =这是测试用户体验

删除用户体验部分。没有删除用户体验如何分割字符串



请协助

I have a string
string source = "Current Status: this is a test current status.User Experience: This is a test user experience"

I want to split this string to two sub strings

string stra = "Current Status: this is a test current status."
string strb = "User Experience: This is a test user experience"

i have tried with these methods

string[] stringSeparators = new string[] { "User Experience" };
var result1 = source.Split(stringSeparators, StringSplitOptions.None);

var rx = new System.Text.RegularExpressions.Regex("User Experience");
var array = rx.Split(source);

But i am getting the result like

string stra = Current Status: this is a test current status.
string strb = This is a test user experience
its removing the "User Experience" part. Without removing the "User Experience" how to split the strings

Please Assist

推荐答案

尝试类似这:

Try something like this:
string source = "Current Status: this is a test current status.User Experience: This is a test user experience";
int index = source.IndexOf("User Experience");
string stra = (index == -1) ? source : source.Substring(0, index);
string strb = (index == -1) ? string.Empty : source.Substring(index);


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

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