Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Verify that one character in a string is a number.

How would one do this? Cheers.

Note: Limited knowledge on vb.net

Any help is appreciated, Cheers!
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?
(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
(Jun 2, 2017 02:19 AM)EMJAAY Wrote:  Im trying to pick out a number within a long string eg:

test1test = HasNum

testtest = NoNum

Code
Dim hasNumber As Boolean = False
        Dim input As String = "test1test"

        Dim characters() As Char = input

        For Each c As Char In characters

            If IsNumeric(c) Then
                hasNumber = True
                Exit For
            End If
        Next

        MessageBox.Show(hasNumber.ToString())

This 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.
(Jun 2, 2017 02:47 AM)Tkings9876 Wrote:  
Code
Dim hasNumber As Boolean = False
        Dim input As String = "test1test"

        Dim characters() As Char = input

        For Each c As Char In characters

            If IsNumeric(c) Then
                hasNumber = True
                Exit For
            End If
        Next

        MessageBox.Show(hasNumber.ToString())

This 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.

Testing it now. Thanks!
A good approach would be possibly using a Regex too, even though give method above is working.

Code
Dim emjd7 = "emjd7"
If Regex.IsMatch(emjd7, "(\d+)") Then
    Console.WriteLine("yes")
Else
    Console.WriteLine("No")
End If

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
Assert failure(PID 10704 [0x000029d0], Thread: 10960 [0x2ad0]): pBuffer != NULL
(Jun 2, 2017 04:20 AM)EMJAAY Wrote:  Testing it now. Thanks!



Very good Baqa ...
(Jun 2, 2017 05:07 AM)Aristocles Wrote:  A good approach would be possibly using a Regex too, even though give method above is working.

Code
Dim emjd7 = "emjd7"
If Regex.IsMatch(emjd7, "(\d+)") Then
    Console.WriteLine("yes")
Else
    Console.WriteLine("No")
End If

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

Regex is a bit overkill.

The Any method from the LINQ namespace like Orka suggested is the way to go.
(Jun 3, 2017 10:27 AM)Ninja Wrote:  Regex is a bit overkill.

The Any method from the LINQ namespace like Orka suggested is the way to go.

What do you mean by 'overkill' ? Also LINQ syntax may not be noob-friendly enough hence why I shared regex solution
Assert failure(PID 10704 [0x000029d0], Thread: 10960 [0x2ad0]): pBuffer != NULL
(Jun 3, 2017 10:32 AM)Aristocles 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.
(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.
Assert failure(PID 10704 [0x000029d0], Thread: 10960 [0x2ad0]): pBuffer != NULL
(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.
(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.
https://github.com/Washi1337

"I'll tell you this UDP joke, and I don't care if you will get it or not."
(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.
the simple way Char.IsNumber
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by MordiciaNov 15, 2019 07:21 PM 3 211 Nov 15, 2019 07:21 PM
Last Post: Mordicia
[Python] by FaberCastellNov 7, 2018 03:38 PM 11 338 Nov 7, 2018 03:38 PM
Last Post: Qismon
[Python] by Fivepoint0Oct 23, 2018 05:24 PM 3 191 Oct 23, 2018 05:24 PM
Last Post: Fivepoint0