views:

124

answers:

8

How do I find last but one character in a vbstring

for e.g. In the string V1245-12V0 I want to return V

A: 

string.Substring(string.Length - 2, 1);

Eric
+2  A: 

Sorry it's been a while since I did VB so this may not be perfect (and is probably a mixture of C# and VB) but you get the idea:

Dim s = "V1245-12V0"
Dim lastButOneLetter = String.Empty

If s.Length > 1 Then
    'Can only get the last-but-one letter from a string that is minimum 2 characters
    lastButOneLetter = s.Substring(s.Length - 2, 1)
Else
    'do something if string is less than 2 characters
End If

EDIT: fixed to be compilable VB.NET code.

Andy Shellam
+1 for the length check. -1 for using substring.
Joel Potter
@Joel - why -1 for using substring? It's much cleaner and more obvious than either Strings.GetChar... or mystring(...)
Andy Shellam
I thought the OP was pretty clear in asking for a "Char". Since I didn't downvote anyone else for using substring, I'll give you the up. It's worth remembering to do the length check.
Joel Potter
A: 

Use the Substring on the string s which contains 'V1245-12V0'

s.Substring(s.Length - 2, 1);

Hope this helps, Best regards, Tom.

tommieb75
Mind me asking why this was downvoted? As others have mentioned this in similar form...to downvote and not leave a comment is deemed highly rude and ignorant which goes against the spirit of SO. How would you like it if I downvoted your answer, not leave a comment and you did not know what was the reason...?
tommieb75
It was probably from Joel Potter who disagrees with using substring. If you notice every answer that used Substring has been downvoted even though it was a perfectly adequate solution based on the information we were given. A downvote IMO is for an answer that doesn't solve the original problem, not because of somebody's personal preference.
Andy Shellam
@Andy: I posted a comment to his answer....
tommieb75
Nope. Wasn't me. Though your answer is in C# so that might have been it.
Joel Potter
A: 

You can have your own functions like

Function Left(ByVal str as string, byval index as integer) As String

    Left=str.Substring(0,index);
End Function

Function Right(ByVal str as string, byval index as integer) As String

    Right=str.Substring(str.Length-index)
End Function

And use them to get what you need.

Kangkan
+1  A: 
Dim secondToLastChar As Char 
secondToLastChar = Microsoft.VisualBasic.Strings.GetChar(mystring, mystring.Length - 2)

http://msdn.microsoft.com/en-us/library/4dhfexk4(VS.80).aspx

Or just remember that any string is an array of chars;

secondToLastChar = mystring(mystring.Length - 2)
Joel Potter
@Joel Potter - then why don't you amend the question to specify not to use substring instead of downvoting everyone despite a vague question being asked! You have enough rep points to do that!
tommieb75
I didn't downvote everybody. I merely upvoted the best solution (Coehoorn's).
Joel Potter
A: 

If you want to get the last alpha-character in a string you could use a LINQ query such as (C#):

var d = from c in myString.ToCharArray().Reverse()
                where Char.IsLetter(c)
                select c;
return d.First();
JoshBerke
See question: VB String.
NTDLS
A: 

Was it difficult?

dim mychar as string
dim yourstring as string
yourstring="V1245-12V0"
mychar=yourstring.Substring(yourstring.Length - 2, 1)
Mani
+4  A: 

Don't use substring to get just one character

Dim MyString As String = "V1245-12V0"
Dim MyChar As Char = MyString(MyString.Length - 2)
Joel Coehoorn
I agree. Can't see why 80% of the answers chose that method.
Joel Potter
It's more flexible based on what the OP wants to do with that character later. The only reason I would use char in favour of string is unless I was absolutely 1000% certain that it would ALWAYS be a char that's required and someone won't come along and say "actually I now want 2 characters from the string" or I was desperately short of memory, such as an embedded device.
Andy Shellam