在C#中的字符串分割 [英] string split in c#

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

问题描述

我有这样的字符串:

  00123_MassFlow 
0022245_VOlumeFlow
122_447_Temperature

我要分开使用C#_ 这些字符串与 _ 可能会出现多个地方,但我得把最后一个值



我的应该是这样的:

 质量流量
VOlumeFlow
的温度

我怎样才能做到这一点?


解决方案

 122_447_Temperature.Split(_)最后()。 

如果你不介意创建一个数组,并扔掉了一堆字符串的额外开销。这不会是一样快,使用 LastIndexOf 子串手动,但它是一吨更容易阅读和维护, IMO。


I have strings like this:

    00123_MassFlow
    0022245_VOlumeFlow
    122_447_Temperature

I have to split these strings with _ using C#. _ may appear multiple places but i have to take last value.

My should be like this:

    MassFlow
    VOlumeFlow
    Temperature

How i can achieve this?

解决方案

"122_447_Temperature".Split('_').Last();

If you don't mind the extra overhead of creating an array and throwing away a bunch of strings. It won't be as fast as using LastIndexOf and Substring manually, but it's a ton easier to read and maintain, IMO.

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

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