How do i pick out specific strings from a user input? - vb.net

I want to make the user input a 3 digit number (100 - 999), then take out the 1st digit, 2nd and 3rd and say what unit it belongs to
e.g.
567 will make
5 hundreds
6 tens
7 ones
I've tried mid and right but mid shows me all my units and right only works for the ones
Im stuck on the tens
Module Module1
Dim Num As Integer
Sub Main()
Console.WriteLine("Enter a number between 100 and 999")
Num = CStr(Console.ReadLine)
Console.Write(Left(Num, 1))
Console.Write(" Hundreds")
Console.WriteLine(Mid(Num, 1))
Console.Write(" Tens")
Console.WriteLine(Right(Num, 1))
Console.Write(" One's")
Console.ReadKey()
End Sub
End Module

Use string.SubString.
For example:
Dim strNumber As String = Num.ToString()
Dim hundreds As String = strNumber.SubString(0, 1) 'shows the hundreds.
Dim tens As String = strNumber.SubString(1, 1) 'shows the tens.
Dim ones As String = strNumber.SubString(2, 1) 'the ones.
As somebody commented on your question:
In your case since you only want one digit to return you can access the strings char array and return the char at position X like so:
Dim strNumber As String = Num.ToString()
Dim hundreds As String = strNumber(0)
Dim tens As String = strNumber(1)
Dim ones As String = strNumber(2)

Related

Separate a String into Two parts 'name' and 'number' using VBA

I need to separate following strings into Name and Number: e.g.
evil333 into evil and 333
bili454 into bili and 454
elvis04 into elvis and 04
Split(String, "#") ' don't work here because numbers are unknown
similarly
Mid(String, 1, String - #) ' don't work because Numbers length is unknown
so what should be the best way to start? Just want to keep it simple as possible
Update:
For further info follow - https://youtu.be/zjF7oLLgtms
Two more ways for solving this:
Sub test()
Dim sInputString As String
Dim i As Integer
Dim lFirstNumberPos As Long
sInputString = "evil333"
'loop through text in input string
'if value IsNumeric (digit), stop looping
For i = 1 To Len(sInputString)
If IsNumeric(Mid(sInputString, i, 1)) Then
lFirstNumberPos = i
Exit For
End If
Next i
Dim Name As String
Dim Number As String
'return result
Name = Left$(sInputString, lFirstNumberPos - 1)
Number = Mid$(sInputString, lFirstNumberPos)
End Sub
Or another method:
Sub test2()
'if you are going to have too long string it would maybe better to use "instr" method
Dim sInputString As String
Dim lFirstNumberPos As Long
Dim i As Integer
sInputString = "evil333"
Dim lLoopedNumber as Long
LoopedNumber = 0
lFirstNumberPos = Len(sInputString) + 1
'loop through digits 0-9 and stop when any of the digits will be found
For i = 0 To 9
LoopedNumber = InStr(1, sInputString, cstr(i), vbTextCompare)
If LoopedNumber > 0 Then
lFirstNumberPos = Application.Min(LoopedNumber,lFirstNumberPos)
End If
Next i
Dim Name As String
Dim Number As String
'return result
Name = Left$(sInputString, lFirstNumberPos - 1)
Number = Mid$(sInputString, lFirstNumberPos)
End Sub
You should regular expressions (regex) to match the two parts of your strings. The following regex describes how to match the two parts:
/([a-z]+)([0-9]+)/
Their use in VBA is thorougly explained in Portland Runner's answer to How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops

How to get the 1st number and the 2nd number in a string?

The string is "What is 8 multiplied by 2?"
How can I get the first 1st number which is "8" and the second number which is "2"?
What I want is to get the first number and use the mathematical operation asked to the second number.
Dim str As String = Label1.Text
Dim i As Integer
For i = 1 To Len(str)
If IsNumeric(Mid(str, i, 1)) Then
Label2.Text = Mid(str, i, 1)
End If
Next
I tried using this but I only get the 2nd number which is "2"
Dim str As String = "What is 8666 multiplied by 2444"
Dim numbers As New List(Of Integer)
For Each word As String In str.Split(" ")
If IsNumeric(word) Then
numbers .Add(CInt(word))
End If
Next
MsgBox("first:" & numbers (0) & ",second:" & numbers (1))

Get last number from string and increase it by one

Have some problems with my function. In database i could have diffrent numbers. For instance below: ( i know it looks strange )
12 312323.3
013.43.9
3.23.14353.55 WHATEVER 345.193
728937.3
87.3 ojojo 23.434blabla 24.424.7
What i need to do is increase number after LAST DOT so just make + 1.
The problem is its not working when it comes after dot more than one digit then.
here is my current code:
Dim inputValue as String = "34.234234.6.12"
'--Get Last char from string and add 1 to it
Dim lastChar As String = CInt(CStr(inputValue.Last)) + 1
'--Remove last char and add lastChar
Dim nextCombinNummer As String = lastValue.Nummer.Substring(0, lastValue.Nummer.Length - 1) & lastChar
Return nextCombinNummer
I think the problem is lastValue.Last + 1 as it will take only one digit, and also when i remove by substring last digit but only 2 will be removed.
Can you help me out with this? How to always take number after last dot from string and then increase that number by 1 and return new entire number?
EDIT:
I think i am able to get and increase the number but still dont know how to remove and put it at the end:
Think that's ok:
Dim inputValue as String = "34.234234.6.12"
Dim number As String = inputValue .Substring(inputValue .LastIndexOf("."c) + 1)
Dim numberIncreased as integer = CInt(number) + 1
'How to do this correctly? :
Dim nextCombinNummer As String = lastValue.Nummer.Substring(0, lastValue.Nummer.Length - 1) & numberIncreased
An easy solution is to cast as Integer the last part of the string, add one, then recompose your string :
'Original Value
Dim val As String = "123.456.789"
'We take only the last part and add one
Dim nb = Integer.Parse(val.Substring(val.LastIndexOf(".") + 1)) + 1
'We recompose the string
Dim FinalVal As String = val.Substring(0, val.LastIndexOf(".") + 1) & nb.ToString()
I'd use following which uses String.Split, Int32.TryParse and String.Join:
Dim numbers As New List(Of String) From {"12.312323.3", "013.43.9", "3.231435355345.193", "728937.3", "87.323.43424.424.7"}
for i As Int32 = 0 To numbers.Count -1
Dim num = numbers(i)
Dim token = num.Split("."c)
dim lastNum = token.Last() ' or token(token.Length-1)
Dim n As Int32
If int32.TryParse(lastNum, n)
n += 1
token(token.Length-1) = n.ToString()
End If
numbers(i) = string.Join(".", token)
Next

VB.net If or statement

I have a textbox that people enter a number or a range, eg 12-15, and a random number is generated. Currently if the second number is less than the first I can get it to work how I want but not if only a single number is listed.
rnum1 should equal rnum2 if there isn't a words(1) or if it's less than words(0). (I do have it done if it's less.)
Dim words As String() = TextBox2.Text.Split("-")
Dim rnum1 As String = words(0)
Dim rnum2 As String = words(1)
Dim RandomClass As New Random()
Dim RandomNumber As Integer
If rnum2 < rnum1 Then
rnum2 = rnum1
End If
RandomNumber = RandomClass.Next(rnum1, rnum2)
Change this line
Dim rnum2 As String = words(1)
to
Dim rnum2 As String = IF(words.Length = 2, words(1), rnum1)
It checks whether there're 2 elements in words array. If it is - it uses second element of array, otherwise it reassigns the first.
Come to think of it, this can be achieved even by this:
Dim rnum2 As String = words(words.Length - 1)
If there're 2 elements in the array - it will assign words(1), otherwise words(0)

Populating 2 dimensional array using a For loop

Currently I'm trying to fill a 3x3 square with random x's and o's to make a tic tac toe
game. Unfortunately the game doesn't seem to output all the x's and o's. Logically, from what I can see, it should be able to but it's not. Any help would be appreciated.
Shared Sub twodimension()
Dim tic(2, 2) As String
Dim min As Integer
Dim x As String
Dim random As New Random()
Dim i As Integer
Dim x1 As Integer
Dim bound0 As Integer = tic.GetUpperBound(0)
Dim bound1 As Integer = tic.GetLowerBound(1)
For i = 0 To bound0
For x1 = 0 To bound1
min = random.Next(2)
If min = 0 Then
x = "x"
Console.WriteLine("{0}", x)
Else
x = "o"
Console.WriteLine("{0}", x)
End If
Console.Write(" "c)
Next
Console.WriteLine()
Next
End Sub
So presumably you've got this declaration somewhere, right?
Public Shared Tic(2, 2) As String
In your code you've got GetLowerBound which will (almost) always returns zero and instead you should have GetUpperBound().
Dim bound0 As Integer = tic.GetUpperBound(0)
Dim bound1 As Integer = Tic.GetUpperBound(1)
EDIT (in response to comment)
GetUpperBound(int) returns the highest number that you can use for the dimension that you specify.
So for the following array:
Dim MyArray(4, 6, 8) As Integer
Trace.WriteLine(MyArray.GetUpperBound(0)) ''//Returns 4
Trace.WriteLine(MyArray.GetUpperBound(1)) ''//Returns 6
Trace.WriteLine(MyArray.GetUpperBound(2)) ''//Returns 8
GetLowerBound(int) returns the lowest number that you can use for the dimension that you specify. In almost every case this is zero but in older versions of VB (and using some COM interop) you can create arrays that don't "start" at zero and instead start at whatever you wanted. So in old VB you could actually say Dim Bob(1 To 4) As Integer and GetLowerBound(0) would return 1 instead of 0. For the most part there is no reason to even be aware that GetLowerBound exists even.