在 C# 中每次都在特殊字符之前剪切一个字符串 [英] Cutting a String before a special character everytime in C#

查看:30
本文介绍了在 C# 中每次都在特殊字符之前剪切一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有不同长度的字符串.例如

str1 = "这是新工作----第一条消息-----这是重新打开的";str2 = "启动进程----第一条消息-----这个进程关闭了吗?<br/>没有----第一条消息-----";

现在这些字符串将始终包含 "----First Message-----" .我需要以这样的方式修剪或拆分字符串,以便我只得到 "----First Message-----" 发生的第一次剩下的部分.>

所以如果 str1 结果应该是这是新工作"对于 str2 应该是 "Start Process "

如何在 C# 中做到这一点?

解决方案

string stringStart = str1.Substring(0, str1.IndexOf("----First Message-----"));

I have strings that all have different lengths. e.g.

str1 = "This is new job ----First Message----- This is reopened ";
str2 = "Start Process ----First Message----- Is this process closed? <br/> no ----First Message-----";

Now these string shall always have the "----First Message-----" in it. What I need is to trim or split the string in such a way that I only get the part left of the FIRST TIME the "----First Message-----" occurs.

So in case of str1 result should be "This is new job " For str2 it should be "Start Process "

How can this be done in C#?

解决方案

string stringStart = str1.Substring(0, str1.IndexOf("----First Message-----"));

这篇关于在 C# 中每次都在特殊字符之前剪切一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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