字符串分析,提取数字和字母 [英] String parsing, extracting numbers and letters

查看:222
本文介绍了字符串分析,提取数字和字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是解析字符串并提取数字和字母的最简单的方法?我有串,可以按以下格式(数字|字母或字母|数),即10A,B5,C10,1G,等等。

What's the easiest way to parse a string and extract a number and a letter? I have string that can be in the following format (number|letter or letter|number), i.e "10A", "B5", "C10", "1G", etc.

我需要提取2部分,即10A - >10和A

I need to extract the 2 parts, i.e. "10A" -> "10" and "A".

更新:。感谢大家为所有优秀的答案

Update: Thanks to everyone for all the excellent answers

推荐答案

最简单的方法可能是使用正则表达式。

Easiest way is probably to use regular expressions.

((?<number>\d+)(?<letter>[a-zA-Z])|(?<letter>[a-zA-Z])(?<number>\d+))

您可以再搭配。它与你的字符串和提取组​​值

You can then match it with your string and extract the value from the groups.

Match match = regex.Match("10A");
string letter = match.Groups["letter"].Value;
int number = int.Parse(match.Groups["number"].Value);

这篇关于字符串分析,提取数字和字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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