how to i create a Username shortener? - vb.net

I have an AD username called "lastname-132" in Textbox1, this string is 12 long, so i want to add the username in to Textbox2, but shortened, in the textbox2 i only have a string length of only 10 available due to other tools this program is using, so i don't want to convert it all the time manually and want to just convert it automatically with a onleave event.
Anyone any idea how to write this?
So the End Result should look like this.
'String length can be 20 max.
Textbox1.Text = "lastname-123"
'some code to convert it to this:
'String length 10 max. Numbers and the "-" should stay the same, but remove letters if necessary.
Textbox2.Text = "lastna-123"

Here's the concept:
Split string based on '-' into 2 strings
In the example above: 'lastname' and '123'.
Check the length of the first string and cut if it is too long
the program checks 'lastname' and finds that it is too long, then
cuts it into 'lastna'
Combine 'lastna' and '123' back into a string
I hope this helps

Without more information, this will assume that there can be multiple hyphens, the number can be of variable length, and you can change the maximum length of the string by changing one variable.
Dim username As String = "lastname-123"
Dim max As Integer = 10
Dim lindex As Integer = username.LastIndexOf("-")
Dim numberLength As Integer = username.Length - lindex
Dim number As String = username.Substring(lindex)
Dim justName As String = username.Substring(0, lindex)
If justName.Length + numberLength >= max Then
username = justName.Substring(0, max - numberLength) & number
End If

If you are concentrating only on the restriction of length of characters to be accepted then you can use
Maxlength
property of the Textbox.
Ex: Maxlength="10"
restricts the Textbox to accept only 10 characters.

Try to make it fit with for example substring manipulation. See http://msdn.microsoft.com/en-us/library/dd789093.aspx for more info.

Related

using IndexOf in Mid function

Perhaps this is a simple solution for most, but I can't get this to work like it should according to syntax.
I have this line of text "Part Number123456Price$50.00"
I want to pull the part number out of it, so I use this function...
str = Mid(str, str.IndexOf("Part Number") + 12, str.IndexOf("Price"))
My results are str = "123456Price$50.0" every time. I know the part number can vary in length so I need a solid solution of pulling this out.
It can be confusing to mix the legacy VB string methods (such as Mid) with the .Net string methods (like IndexOf). The VB methods use 1 as the index of the first character while the .Net methods use 0.
The following code will extract the part number from a string
Dim str As String = "Part Number123456Price$50.00"
Dim iPart As Integer = str.IndexOf("Part Number") + 11
Dim iPrice As Integer = str.IndexOf("Price")
str = str.Substring(iPart, iPrice - iPart).Trim
The Mid() function of Visual Basic is documented as having three arguments: (1) a string, (2) the beginning location in the string, and (3) the number of characters to copy.
So if your string is "Part Number123456Price$50.00" and you want to pull the part number as a series of digits, the "123456" part of the string, using the Mid() function then you need to find the beginning of the part number digit string and to then know the number of digits.
If your string is in the variable str then you can find the offset by something like str.IndexOf("Number") + len("Number") which will provide the offset to after the string "Number".
Next you need to find the number of digits so you would do something like str.IndexOf("Price") to find where the text "Price" begins and then subtract from that offset the offset of where the digits begin.
The result of all of this is you need a bit of code something like the following. I have not tested this source as I am not a VB programmer so it may need a tweak and you might want to put some checks on data validity as well.
Dim TextNumber as String = "Number"
Dim TextPrice as String = "Price"
iOffset = str.IndexOf(TextNumber) + len(TextNumber)
str = Mid(str, iOffset, str.IndexOf(TextPrice) - iOffset)
Alternatively, if Price is always the format $00.00, this will also work.
Dim str as String = "Part Number123456Price$50.00"
str = str.Remove(str.IndexOf("Price"))

How to verify an Integer? VB.Net

How do verify a value entered in an input box is an integer? If it's not I want the input box to show up again no harm done. Thanks for any help in advance
Dim int As Integer
If Integer.TryParse("12345", int) Then
'use int variable it holds the converted value
End If
as #OneFineDay said you have to use Integer.TryParse, now to repeat the question untill it is an integer you can do this:
dim ans = As String
dim int As Integer
dim isInteger As Boolean = False
do While Not isInteger
ans = InputBox("Give me an Integer")
isInteger = Integer.TryParse(ans, int)
End do
''Here int holds an integer
It wouldn't be an input validation question without a Regex answer, so if you want to make it more complicated than it needs to be then you can use something like
Dim expression As New Regex("^-?\d+$")
If Not expression.IsMatch(textBox1.Text) Then
textBox1.Text = String.Empty
End If
The Regex pattern will look at all entered text, and match iff there is zero or one minus signs followed by at least one digit.
12345 represents text that you can either enter manually in quotes or can grab from an input box like this:
Integer.TryParse(InputBox("Enter integer here"),myInt) ,such that the input from the inputbox is the one that will be converted into the integer variable,this saves you memory no need to declare another variable.
The loop in #bto.rdz 's answer is quite handy though,especially if you want the user to enter an integer no matter what

What method can I use to retrieve a string using two character indexes?

Just like the title says; I want to use something like Mid(stringName, startIndex,[integerLength]), but instead of the third argument taking a string length, I want it to take the end character index. So in an example like this
alphabet = "ABCDEFG"
partial = *method I want to use*(alphabet, 2, 4) 'partial would equal "BC"
(Forgive me if my index numbers are off, but I hope you get my point.)
Does something like this exist in VB.NET?
You'll want to use String.Substring
http://msdn.microsoft.com/en-us/library/aka44szs.aspx#Y0
dim alphabet as string = "ABCDEFG"
'partial is a reserved word!
'1,2 is the correct parameters to get 'BC'
dim partialString as string = alphabet.Substring(1, 2) 'partial would equal "BC"
Edit - Oooooh you want to do StartIndex,StopIndex not StartIndex,Length. Just apply a bit of math.
dim startIndex as integer = 1
dim stopIndex as integer = 3
'partial would equal "BC"
dim partialString as string = _
alphabet.Substring(startIndex , stopIndex-startIndex )
I'd wrap that in an extension method on string, giving it a new name of course.
Just use Mid, the math for the length is quite easy (length = endIndex - startIndex):
part = Mid(alphabet, 2, 4-2)
You could also achieve the same thing with Substring (which uses 0 based indexes rather than 1 based):
part = alphabet.Substring(1, 3-1);
targetstring=alphabet.Substring(2,4)
The above one should work..

splitting a string to access integer within it

i have a string "<PinX F='53mm'></PinX>", I want to access the 53 within the string and do some addition to it and then add the answer back into that string. I've been thinking about this and wasn't sure whether this can be done with regular expression or not? Can anybody help me out.
thanks
Yes, you can use a regular expression. This will get the digits, parse them to a number, add one to it, and put it back in the string (that is, the result is actually a new string as strings are immutable).
string s = Regex.Replace(
input,
#"(\d+)",
m => (Int32.Parse(m.Groups[1].Value) + 1).ToString()
);
Take a look at the HTML Agility Pack.
A regular expression looks like a good fit for this particular problem:
\d+
Will match one or more digits.
Int32.Parse(Regex.Match("<PinX F='53mm'></PinX>", #"\d+").Value)
Will return 53.
In this single case yes. "'(.*?)' then access the first group, but if this is part of a larger xml regular expressions should not be used. You should utilize the xml parser build into .net find the attribute with xsd and get the value.
Alternatively, here's a small routine...
' Set testing string
Dim s As String = "<PinX F='53mm'></PinX>"
' find first occurence of CHAR ( ' )
Dim a As Integer = s.IndexOf("'")
' find last occurence of CHAR ( ' )
Dim b As Integer = s.LastIndexOf("'")
' get substring "53mm" from string
Dim substring As String = s.Substring(a, b - a)
' get integer values from substring
Dim length As Integer = substring.Length
Dim c As Char = Nothing
Dim result As String = Nothing
For i = 1 To length - 1
c = substring.Chars(i)
If IsNumeric(c) Then
result = result & c
End If
Next
Console.WriteLine(Int32.Parse(result))
Console.ReadLine()

How to strip a string of all alpha's?

Dim phoneNumber As String = "077 47578 587(num)"
How do i strip the above string off every character which isnt a number. So only the numbers are left and then check to make sure it is 11 characters long?
dim number as string = Regex.Replace(phoneNumber,"[^0-9]","")
if number.length = 11 then
'valid number
else
'not valid
end if
You could loop on each character and check if it is a digit. While looping, check that the number of accepted characters (digits) is less than 11.
or
use a regex to remove all the alpha but you still will have to count at the end ....
Dim phoneNumber As String = "077 47578 587(num)"
Dim newPhoneNumber = String.Empty
For i = 0 To phoneNumber.Length - 1
If IsNumeric(phoneNumber(i)) Then
newPhoneNumber += phoneNumber(i)
End If
Next
Dim valid = newPhoneNumber.Length = 11
One possible solution is to treat the string as a character array, then retrieve only those characters with ascii codes within the paramaeters you define.
Ascii codes can be found at a resource such as: http://www.bolen.net/html/misc/ASCII-codes.html
Alternatively, you could use a regular expression to retrieve only the characters you want. My regex isn't so hot, so I can't give an example :)