Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
dim strMyString as string ="ABC POR BCS KOK SSS 6BC"
strMyString=strMyString.Substring(19)
strMyString=strMyString.Substring(2, 1).ToString

I need to find Only B Value which is in middle of Last String i.e 6BC
but i am getting eror on this line
strMyString.Substring(2, 1).ToString

Help me
Posted
Comments
DipAnikap 5-Apr-14 7:58am    
Your string is fixed?
CHill60 5-Apr-14 8:50am    
This code works and does not give an error. So presumably the string can vary (see comment from DipAnikap above). Peter Leow has given a good explanation of your problem in Solution 4. But what are you actually trying to achieve? Is it "find the last 'B' in any string" or "find the last occurrence of character X in any string Y"?

Your question is not complete.
If the "6BC" is present in the string, your program will work fine.
But if the "6BC" is absent, then your "strMyString=strMyString.Substring(19)" returns empty string, that is why "strMyString=strMyString.Substring(2, 1).ToString" throws error as there is no index 2.
There is no need to use ToString in the first place, but if you insist, then
ToString and ToString() are the same as in VB.NET you can omit the parentheses on any method that has no argument.

If your intention is to check if there is any "B" exists after index 19, then you can use lastindexof like the following example:
VB
dim strMyString as string ="ABC POR BCS KOK SSS 6aC"
dim index as integer =strMyString.LastIndexOf("B")
'strMyString=strMyString.Substring(2, 1).ToString
If index > 19 Then
    Console.WriteLine("There is B after index 19")
Else
    Console.WriteLine("There is no B after index 19")
End If

Test with B and no B and see how it works.
 
Share this answer
 
v4
Comments
CHill60 5-Apr-14 8:47am    
5'd. As you say, the OPs code as presented here does not throw the exception they mention.
Peter Leow 5-Apr-14 8:51am    
Thanks Chill60. OP has something else in mind that was not mentioned in the question. The program as it stands is fine.
if you need to find last B, then try to find last stirng as follow:

VB
Dim s As String = "ABC POR BCS KOK SSS 6BC"
        Dim i As Integer = s.LastIndexOf("6"c) + 1
        Dim part As String = s.Substring(i)
        lbl.Text = part
 
Share this answer
 
v2
I think it should be
VB
strMyString=strMyString.Substring(2, 1).ToString() ' with parentheses

but why are you calling ToString on a string?
 
Share this answer
 
Its ToString().
Try strMyString.Substring(2, 1).ToString().
 
Share this answer
 
Comments
hareshdgr8 5-Apr-14 8:12am    
just converting value to string if its not there is no problem but it give me like starting index cannot be more than end index something like this..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900