![]() |
|
[.NET] Verify that one character in a string is a number. - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [.NET] Verify that one character in a string is a number. (/showthread.php?tid=5637373) Pages: Pages
1
2
|
Verify that one character in a string is a number. - EMJAAY - Jun 2, 2017 How would one do this? Cheers. Note: Limited knowledge on vb.net Any help is appreciated, Cheers! * - Tkings9876 - Jun 2, 2017 Does your string only consist of a single character or are you picking out a specific character within a string and wanting to see if it's a number? * - EMJAAY - Jun 2, 2017 (Jun 2, 2017 01:14 AM)Tkings9876 Wrote: Does your string only consist of a single character or are you picking out a specific character within a string and wanting to see if it's a number? Im trying to pick out a number within a long string eg: test1test = HasNum testtest = NoNum * - Tkings9876 - Jun 2, 2017 (Jun 2, 2017 02:19 AM)emjd- Wrote: Im trying to pick out a number within a long string eg: Code Dim hasNumber As Boolean = FalseThis will loop through each character of the string "input". If it finds a numeric character the hasNumber boolen will be set to true and the loop will exit. If no numeric characters have been found hasNumber will be false. Messagebox.Show to display the outcome. Hope this helps. * - EMJAAY - Jun 2, 2017 (Jun 2, 2017 02:47 AM)Tkings9876 Wrote: Testing it now. Thanks! * - Aristocles - Jun 2, 2017 A good approach would be possibly using a Regex too, even though give method above is working. Code Dim emjd7 = "emjd7"Here I have a string, emjd7, which contains a number, 7. Regex (\d+) stand for AnyDigit in the given string. Here is full source code to test it out : https://dotnetfiddle.net/OSpmyl I built my regex using this website : https://txt2re.com/index-vb.php3?s=ggfgdfg5&4 you enter a string an you can click on anything to produce regex, really fast-productive ! :) For a better approach than mine on Regex, see https://www.dotnetperls.com/regex-vbnet * - darkdoctor - Jun 3, 2017 (Jun 2, 2017 04:20 AM)emjd- Wrote: Testing it now. Thanks! Very good Baqa ... * - Ninja - Jun 3, 2017 (Jun 2, 2017 05:07 AM)Martin Heidegger Wrote: A good approach would be possibly using a Regex too, even though give method above is working. Regex is a bit overkill. The Any method from the LINQ namespace like Orka suggested is the way to go. * - Aristocles - Jun 3, 2017 (Jun 3, 2017 10:27 AM)Ninja Wrote: Regex is a bit overkill. What do you mean by 'overkill' ? Also LINQ syntax may not be noob-friendly enough hence why I shared regex solution * - Ninja - Jun 3, 2017 (Jun 3, 2017 10:32 AM)Martin Heidegger Wrote: What do you mean by 'overkill' ? Also LINQ syntax may not be noob-friendly enough hence why I shared regex solution Regex is a bit too heavy for just checking if a string contains a digit. * - Aristocles - Jun 3, 2017 (Jun 3, 2017 10:45 AM)Ninja Wrote: Regex is a bit too heavy for just checking if a string contains a digit. I still don't understand, "overkill", now "too heavy" ❓ Google "check if string contains digit c#" will underlines two possibilities : LINQ and Regex. Both are good, I don't think Regex uses that much memory compared to LinQ though I may be wrong. * - +Haze - Jun 3, 2017 (Jun 3, 2017 10:45 AM)Ninja Wrote: Regex is a bit too heavy for just checking if a string contains a digit. Regex isn't overkill, it's literally designed for tasks like these. Regex is mainly for pattern matching. If anything, Linq is overkill due to the overheads. * - Washi - Jun 3, 2017 (Jun 3, 2017 12:20 PM)+Haze Wrote: Regex isn't overkill, it's literally designed for tasks like these. Regex is mainly for pattern matching. If anything, Linq is overkill due to the overheads. I very much doubt that since regular expressions are converted to entire method bodies that need to be assembled, whereas LinQ is just a simple lazy iterator. * - +Haze - Jun 3, 2017 (Jun 3, 2017 04:06 PM)Washi Wrote: I very much doubt that since regular expressions are converted to entire method bodies that need to be assembled, whereas LinQ is just a simple lazy iterator. Eh, sounds about right, I forgot regex are complex little things behind the scenes. * - HFDone - Jun 3, 2017 the simple way Char.IsNumber |