How do you get the number that is in the center of numbers entered in a textbox vb.net

Dominador Schneider

For example I entered the numbers "31572" in a textbox then it will get the number "5". Also if I enter numbers with the length of even numbers like "123752" then it will get the "37" which is in the center of the entered numbers. I have no idea what to with this, so I'm hoping someone gives me an idea. Thank you :)

Jimi

Since you want to take from the string 2 chars when the string length is even and 1 char when it's odd, you just need to care about the remainder of the division by 2.

Using Integer division (3 \ 2 = 1):
If String.Length Mod 2 = 0, take 2 chars, starting from String.Length \ 2 - 1
If String.Length Mod 2 = 1, take 1 chars, starting from String.Length \ 2

You may want to add a null check and just return the original string if the string length is less than 3 (when the string is "1" or "12", return the string as it is).
Your code could be:

Dim result as string = GetCenterValue(TextBox1.Text)

Public Function GetCenterValue(content As String) As String
    If String.IsNullOrEmpty(content) Then Return String.Empty
    Dim sLength As Integer = content.Length
    If sLength < 3 Then Return content
    Return content.Substring(sLength \ 2 - (1 - sLength Mod 2), 2 - sLength Mod 2)
End Function

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do you change the value in my.settings in a form when you enter numbers in a textbox in VB.Net

How to get number of line of a specific word in it in multiline textbox in vb .net

How do you get a .Net Maui Map Center Location?

how do you pass the value/string of textbox to a XtraLabel in XtraReport in vb.net?

how can I store specific characters that were entered into a textbox in a variable in vb.net?

how to change label to red if number entered into textbox

How do you make an event happen when text is entered into a textbox in C#

How to get the value on the textbox in server vb.net

how to get data from database and put it on the textbox - vb.net

How do I center the numbers for this C triangular number generator?

How do you get all the Textbox from a DataGrid?

How do you get a AjaxFileUpload control to see what is in a textbox on a form

How do you get NSScrollView to center the document view in 10.9 and later?

How do you multiply each digit of a number by different numbers in python?

How do you limit the number of decimal places in the output of multiple numbers?

how do you change the text color in a vb.net chart?

how do you repeat the total entered?

In VB.Net, when using a CheckBox with a Button appearance, how can you truly center-align the text?

How to get line number from a text file in vb.net

VB.NET How to NOT subtract a larger number from a smaller number for textBoxResult using random generated numbers

How do you get a list of numbers from 1 to n in Julia?

How do you get a value of two int numbers from an array

How do I write a do while loop to ask user to enter numbers and terminate the loop when the number entered is 0

How do I prevent a leading space from being entered in a textbox?

How to get the value that I entered in the textbox when click the button?

Get numbers between chars in VB.NET

How do I find the read only backcolor of a textbox in vb.net?

How do I populate the next empty cell with textbox data in excel using vb.net?

How to do the textbox keypress can click button automatically in vb.net