C#中的子串词 [英] Substring word in C#

查看:26
本文介绍了C#中的子串词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 c# 中的结果文本中获取子字符串 XXXZZZ
文本形式:("; XXX; ZZZ; WWW") 但是

I want to get the substring XXX and ZZZ from my result text in c#
Text form: ("; XXX; ZZZ; WWW") but

Result.LastIndexOf(";")

不影响,因为我有 ";" 单独的两个单词的字符我在我的话中找不到第一个和第二个 ";" 字符的索引.

Does not affect because I have ";" char for separate two word and I can't find index of first and second ";" char in my word.

推荐答案

使用 string.Split() 拆分输入字符串并使用 string.Trim() 删除周围的空格:

Split input string using string.Split() and remove spaces around using string.Trim():

var str = "; XXX; ZZZ; WWW";
var parts = str.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
               .Select(x => x.Trim());
foreach (var part in parts)
    Console.WriteLine(parts);

结果与问题中所问的完全一样,并且没有空值:

Result is exactly as asked in the question and without empty values:

XXX
ZZZ
WWW

这篇关于C#中的子串词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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