从一个字符串分割特殊字符和字母 [英] split special character and alphabets from a string

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

问题描述

我有一个字符串值。予含有alphbets,特殊字符和数字,whitspace的组合。但我想只提取号码。

 我的code
-------
昏暗STR1的String =@#$ @#$#123456habAB ^ * ^&放大器;!!!(*)(_)()*(
    昏暗的STR2的String = Regex.Replace(STR1,[\ [\] \\\ ^ \ $ \ \ | \ \ * \ + \(\)\ {\}%;?!>< @ #&安培; \  -  \ + /天],)

    MSGBOX(STR2)


输出我得到
-----------------
123456habAB_

预计产量
---------------
123456
 

解决方案

试试这个code,短期和简单的:

 昏暗STR1的String =@#$ @#$#123456habAB ^ * ^&放大器;!!!(*)(_)()*(
昏暗的STR2的String = Regex.Replace(STR1,[^ \ D],)

MSGBOX(STR2)
 

您的解决方案的问题是,你并没有取代_或字母与空字符串。它更容易从一个字符串不是明确替换所有字符替换所有非数字。

am having a string value . I contain the combination of alphbets, special char and numbers and whitspace. but i want to retrieve only numbers.

my code
-------
Dim str1 As String = "!@!@#!$@#$#123456habAB^*^&(*)(_)()*("
    Dim str2 As String = Regex.Replace(str1, "[\[\]\\\^\$\.\|\?\*\+\(\)\{\}%,;><!@#&\-\+/d]", "")

    MsgBox(str2)


output am getting
-----------------
123456habAB_

expected output
---------------
123456

解决方案

Try this code, short and simple:

Dim str1 As String = "!@!@#!$@#$#123456habAB^*^&(*)(_)()*("
Dim str2 As String = Regex.Replace(str1, "[^\d]", "")

MsgBox(str2)

The problem with your solution was you did not replace "_" or alphabets with empty string. Its easier to replace all non numbers from a string than replacing all characters explicitly.

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

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