Image

Imagejaboba wrote in Imagevbdev

Basic

Hi.. I've just barely started learning VB. I'm doing a class. One of our assignments was to write a program that would display the exact middle two or middle three (depending on if the text was an even amount of characters or an odd amount) characters from a textbox the user inputs or whatever it is I'm trying to say. You get the point.

Now... I managed to do this with the following... but I am curious as to how else one would get this to work?


Private Sub Command1_Click()
Dim string1 As String
Dim string1_length As Integer
Dim string1_mid As String
Dim string2_length As Integer
Dim string2_mid As String

'Input
string1 = Text1.Text

string1_length = Len(string1)

string1_mid = Mid(string1, string1_length / 2, 2) 'Even amount of characters
string2_mid = Mid(string1, string1_length \ 2, 3) 'Odd amount of characters

If string1_length Mod 2 = 0 Then
Text2.Text = string1_mid
Else
Text2.Text = string2_mid
End If

End Sub


I had to type that out from a sheet so forgive any typos. But I think you get the point.

How else can you get the same result?