如何从字符串中删除非字母字符? [英] How to remove non-alphabetical characters from a string?

查看:37
本文介绍了如何从字符串中删除非字母字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来从任何非字母字符的字符串中删除字符.

I am looking for a way to remove characters from any string that are not alphabetical characters.

我基本上只是对每个非字母字符使用替换.这种方法需要永远.

I am basically just using Replace for every non-Alphabetical character. This method would take forever.

我想我可以制作一个数组(我认为),但这仍然需要很长时间.有什么简单的解决办法吗?

I guess I could make an array (I think) but that would still take quite a while. Is there any simple solution?

Dim wordy As String = textBox.Text.ToUpper.Replace(".", "").Replace("!", "").Replace(" ", "").Replace("'", "").Replace("?", "") _
        .Replace(",", "").Replace("-", "")

推荐答案

以下几行代码应该会有所帮助.

The following lines of code should help.

MsgBox(Regex.Replace(s, "[^a-zA-Z ]", ""))

这将只保留大写/小写 A-Z 以及空格.

This will keep only upper/lowercase A-Z as well as spaces.

你的例子

Dim wordy As String = textBox.Text.ToUpper.Regex.Replace(s, "[^a-zA-Z ]", "")

您也可以只使用 MaskedTextBox,它只允许基于掩码的数字输入.

You could also just use a MaskedTextBox that would allow only numeric input based on the mask.

这篇关于如何从字符串中删除非字母字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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