Getting the character at an index Visual Basic - vb.net

I'm trying to get the character at a specified point in a string and I am proficient in Java but am learning VB for a competition tomorrow. I am trying to get the n char in a string..
for example, I have string "12345" and I want the middle character from the string which is '3' in this case. currently I'm trying to use
middle = Nums.Chars(CInt((Nums.Length / 2))+1)
where middle is a String. It gives me a character but sometimes that character isn't even the middle character in the string. The value of middle is the middle number + 1 so I assumed it was the character code of the char ( 0 = 1, 1 = 2, 2 = 3, etc..) but that was just my theory. So I tried to change it to a String by using
middle = Nums.Chars(CInt((Nums.Length / 2))+1) & ""
Alas that still didn't work so now I come here. All criticism is greatly appreciated.
TL;DR I need to find the middle character in a String, in my case the string is always an integer converted to a string.

Try this:
Public Function GetMiddleString(original As String) As String
If original.Length Mod 2 <> 0 AndAlso original.Length >= 3 Then
Return original.Substring(original.Length \ 2 + 1, 1)
Else If orginal.Length Mod 2 = 0 AndAlso original.Length >= 3 Then
Return original.Substring(original.Length \ 2, 1)
End If
Return original
End Function
Note: This assumes that original is not null (Not Nothing).

As far as I know there is Mid() function to get the middle or the index of the middle char.

Related

Get the nth character, string, or number after delimiter in Visual Basic

In VB.net (Visual Studio 2015) how can I get the nth string (or number) in a comma-separated list?Say I have a comma-separated list of numbers like so:13,1,6,7,2,12,9,3,5,11,4,8,10How can I get, say, the 5th value in this string, in this case 12?I've looked at the Split function, but it converts a string into an array. I guess I could do that and then get the 5th element of that array, but that seems like a lot to go through just to get the 5th element. Is there a more direct way to do this, or am I pretty much limited to the Split function?
In case you are looking for an alternative method, which is more basic, you can try this:
Module Module1
Sub Main()
Dim a As String = "13,1,6,7,2,12,9,3,5,11,4,8,10"
Dim counter As Integer = 5 'the number you want (in this case, 5th one)
Dim movingcounter As Integer = 0 'how many times we have moved
Dim startofnumber, endofnumber, i As Integer
Dim numberthatIwant As String
Do Until movingcounter = counter
startofnumber = InStr(i + 1, a, ",")
i = startofnumber
movingcounter = movingcounter + 1
Loop
endofnumber = InStr(startofnumber + 1, a, ",")
numberthatIwant = (Mid(a, startofnumber + 1, endofnumber - startofnumber - 1))
Console.WriteLine("The number that I want: " + numberthatIwant)
Console.ReadLine()
End Sub
End Module
Edit: You can make this into a procedure or function if you wish to use it in a larger program, but this code run in console mode will give the output of 12.
The solution provided by Plutonix as a comment to my question is straightforward and exactly what I was looking for, to wit:result = csv.Split(","c)(5)In my case I was incrementing a variable each time my program ran and needed to get the nth character or string after the incremented value. That is, if my program had incremented the variable 5 times, then I needed the string after the 4th comma, which of course, is the 5th string. So my solution was something like this:result = WholeString.Split(","c)(IncrementedVariable)Note that this is a zero-based variable.Thanks, Plutonix.

vb.net increment string with prefix

I'm sure this question has been asked before, but I can't find exactly what I'm after.
I have a string which has a string, then a dash, then a number, e.g. "TERM-01" which happens to be the name of an electrical terminal in a switchboard.
I want to increment it to "TERM-02", "TERM-03" etc.
I have come up with this:
TermNo = CStr(Mid(TermNo, 1, InStr(TermNo, "-")) & (CInt(Mid(TermNo, InStr(TermNo, "-") + 1, TermNo.Length - InStr(TermNo, "-")) + 1)))
Which seems to work fairly well, however I need to increment the number including the 0, so 08, 09, 10 instead of 8,9,10.
Any ideas?
You could use the standard Substring method to point to the part where the number starts, convert it to an integer and add your increment. The trick to return the number with the 0 prefix is done using the format specifier D2
Dim TermNo = "TERM-01"
for i = 1 To 15
Dim value = "TERM-" + (Convert.ToInt32( _
TermNo.SubString( _
TermNo.IndexOf("-"))) + i) _
.ToString("D2")
Console.WriteLine(value)
Next
This could also be written using a Regex expression in a more readable way
Dim TermNo = "TERM-01"
for i = 1 To 15
Console.WriteLine(Regex.Replace(TermNo, "\d+", i.ToString("D2")))
Next
If you always have a dash, simply split the string on the dash and deal with the pieces individually.
https://msdn.microsoft.com/en-us/library/system.string.split%28v=vs.110%29.aspx

VB determining values within a string

I am looking for assistance with my program. I have a user enter 6 digits; of these the input must be alpha-numeric. I have already done the TryParse method for the numbers, but I am looking for validation that the string contains an alpha.
I am aware you must use ASC but am unsure both on how to develop a range say Asc((Chr(65) <= Chr(90))) (between A-Z) and also to say (IF my input contains any of these values within the 6 characters, to return true. I keep getting an overload resolution and wish to know how to properly code so the variables are accurate.
This is a great place to use a regular expression
Dim input = ...
If Regex.IsMatch(input, "^\w+$") AndAlso input.Length = 6 Then
' It's a match
Else
' It's not a match
End If
This will match any string which consists only of letters that has length equal to 6
You can iterate through each char and check if it's a letter. If so, set a flag to true.
Dim containsAlpha Boolean = False
For i As Integer = 0 To input.Length - 1
If Char.IsLetter(input(i)) Then
containsAlpha = True
Exit For
End If
Next
Char.IsLetter will match Unicode alphabetic letters, so not just Latin A-Z (which may or may not be what you actually want).

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

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 :)