搜索给定字符串中的数字.

编程入门 行业动态 更新时间:2024-10-15 06:15:09
本文介绍了搜索给定字符串中的数字.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个可能包含数字的字符串.我想编写一个搜索包含数字的子字符串的函数. 我无法搜索字符串中的数字 请向我提供任何有帮助的想法.

I have a string which may contain numbers.I want to write function which searches for substring which contains numbers. I am not able to search for numbers in string Please provide me with any ideas which will help.

推荐答案

尝试这个正则表达式字符串的男孩:\b(?=[-/\\\\A-Za-z]*[0-9])(?=[-/\\\\0-9]*[A-Za-z])[A-Za-z0-9]+(?:[-/\\\\][A-Za-z0-9]+)*\b 它只会找到包含数字的字符串. 这样的字符串"Dam It12不是45 g00d". 会给你: "It12" "not45" "g00d" 例子 Try this bade boy of a regex string: \b(?=[-/\\\\A-Za-z]*[0-9])(?=[-/\\\\0-9]*[A-Za-z])[A-Za-z0-9]+(?:[-/\\\\][A-Za-z0-9]+)*\b It will find only strings that contains numbers. string like this "Dam It12 is not45 that g00d". Will give you: "It12" "not45" "g00d" Example System.Text.RegularExpressions.MatchCollection ms = System.Text.RegularExpressions.Regex.Matches("Dam It12 is not45 that g00d", @"\b(?=[-/\\\\A-Za-z]*[0-9])(?=[-/\\\\0-9]*[A-Za-z])[A-Za-z0-9]+(?:[-/\\\\][A-Za-z0-9]+)*\b"); foreach (System.Text.RegularExpressions.Match m in ms) { Console.WriteLine(m.ToString()); }

string abc = "deepthi12 " char[] SpecialChars = "0123456789";.ToCharArray(); int indexOf = abc.IndexOfAny(SpecialChars); if (indexOf != -1) { //there should a number in the string } else { //No number }

您可以使用Regex.Replace()进行此操作. 例如Regex.Replace(myString, @"\d", string.empty); You can use Regex.Replace() to do this. e.g. Regex.Replace(myString, @"\d", string.empty);

更多推荐

搜索给定字符串中的数字.

本文发布于:2023-11-29 16:03:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646880.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!