在 C# 中简单获取字符串(忽略末尾的数字) [英] Simple get string (ignore numbers at end) in C#

查看:30
本文介绍了在 C# 中简单获取字符串(忽略末尾的数字)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为正则表达式有点矫枉过正,而且我需要一些时间来编写一些代码(我想我现在应该学习一些正则表达式).

I figure regex is overkill also it takes me some time to write some code (i guess i should learn now that i know some regex).

在字母数字字符串中分隔字符串的最简单方法是什么?它将始终是 LLLLDDDDD.我只想要字母(l's),通常只有 1 或 2 个字母.

Whats the simplest way to separate the string in an alphanumeric string? It will always be LLLLDDDDD. I only want the letters(l's), typically its only 1 or 2 letters.

推荐答案

修剪结束:

string result = input.TrimEnd(new char[]{'0','1','2','3','4','5','6','7','8','9'});
// I'm sure using LINQ and Range can simplify that.
// also note that a string like "abc123def456" would result in "abc123def"

但是 RegEx 也很简单:

But a RegEx is also simple:

string result = Regex.Match(input,@"^[^\d]+").Value;

这篇关于在 C# 中简单获取字符串(忽略末尾的数字)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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