2

I am currently working in vb.net express 2013. I am using windows form applications. I need to pull the third from last number in a string of numbers without getting the other numbers behind it. I found this question on this website, Get last 5 characters in a string, which is very close to what I need. However, this code pulls ALL of the last 5 characters, and in my code, I need the third to last without any other numbers. For example, if you take the number "917408," I need to select the "4." With this I am going to create an IF statement based on what number is returned from the original long number.

      'Ghost Floor
    If CBJob1.Visible Then
        If Shear1.Text >= 3 Then
            Dim ghostshear1 As String = Shear1.Text
            Dim len = ghostshear1.Length
            Dim result = ghostshear1.Substring(len - 3, 1)
            MsgBox(result)
        End If
    End If
Community
  • 1
  • 1
Cheddar
  • 530
  • 4
  • 30
  • 3
    you just need to look at that answer and modify the arguments to [substring](http://msdn.microsoft.com/en-us/library/system.string.substring(v=vs.110).aspx) and modify it slightly for your needs. – Ňɏssa Pøngjǣrdenlarp Sep 22 '14 at 19:17
  • 2
    The fourth digit in your string is `myString.Chars(3)` or `myString.Substring(3,1)`. See [here](http://msdn.microsoft.com/en-us/library/aka44szs(v=vs.110).aspx) and [here](http://msdn.microsoft.com/en-us/library/system.string.chars(v=vs.110).aspx) – Robert Harvey Sep 22 '14 at 19:18
  • 1
    `LEFT(RIGHT(string,3),1)` – RBarryYoung Sep 22 '14 at 19:19
  • 1
    Is the number always the same number of digits? – Jason Down Sep 22 '14 at 19:19
  • @ plutonix, Yes i have tried that and I cant get the "str" to work. Robert, does that come from the front end or the back of the string. @ RBarry, Does that select from the back of the string? @ Jason, No the string will not be the same length every time, HOWEVER, the number I need will ALWAYS be the third to last number. – Cheddar Sep 22 '14 at 19:23
  • @RobertHarvey I believe that code is selecting from the front end of the string, which will not always be the same amount of numbers in front of the number I need. I need to come from the back end of the number, or am i missing something on those web sites? – Cheddar Sep 22 '14 at 19:28
  • 1
    Then your position will be string length, minus the number of characters you want to back up, minus one. `myString.Substring(mystring.Length - 3 - 1, 1)` – Robert Harvey Sep 22 '14 at 19:30
  • I just posted my code, the "substring" wont work due to the fact shear1 is coming out of a textbox, should I Dim another variable that is set to the textbox and try it with that? – Cheddar Sep 22 '14 at 19:38
  • I got it, updated code for correct answer – Cheddar Sep 22 '14 at 19:41

3 Answers3

4

Another approach is to convert the string to an integer and then grab the 100s column (third last column in any digit >= 100).

Dim strValue As String = "917408"
Dim number As Int32 = Convert.ToInt32(strValue)
Dim hundredsDigit As Int32
hundredsDigit = (number / 100) Mod 10

If your number is already an actual number (and not a string) this will save you from having to convert it to a string to begin with.

Jason Down
  • 21,731
  • 12
  • 83
  • 117
  • No problem. Just don't forget to put some error checking in there to protect against numbers smaller than 100 (if that is a concern). – Jason Down Sep 24 '14 at 14:08
3

To extract a character in a particular position counting from the end of the string you need to know the length of the string. This is really easy.

Dim test = "917408"
if test.Length >= 3 then
    Dim len = test.Length
    Dim result = test.Substring(len - 3, 1)
End if

Now, you need the 3rd character from the end, so you should add a check to avoid referencing a negative position in case the string is less than 3 characters

The key to your solution is the string class Substring method that takes two parameters:

  • a starting point (len - 3)
  • the number of characters to return (in your case 1)
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Im going to test this code, let me throw some junk data in my sql test server and get back to you on if it works or not. – Cheddar Sep 22 '14 at 19:32
  • I think this code will work, the only problem is, I have a textbox with this value and it is telling me substring can not be used in a textbox. Check the code im about to post. – Cheddar Sep 22 '14 at 19:37
  • 1
    `If Shear1.Text.Length >= 3 Then`. TextBox in WinForms or WPF or? – Steve Sep 22 '14 at 19:39
  • I got it, it is a round about way to do it but its ok. Thanks for your help. – Cheddar Sep 22 '14 at 19:41
2

As Robert Harvey pointed out in the comments above, you just need to alter your Substring arguments:

EDIT: based on @OP's comments about the string changing between 6 and 7 characters:

    Dim strValue As String = "917408"
    Dim newValue As String

    newValue = strValue.PadLeft(7, "0").Substring(4, 1)
    MessageBox.Show(newValue)
SnookerC
  • 160
  • 2
  • 11
  • This actually doesn't work because the length of my number changes for the last 3 months of the year, when we go from month 9 to months 10, 11, and 12. I just tried this code and when we cross the thresh hold if gives me the incorrect number. I need one that selects from the back. – Cheddar Sep 24 '14 at 13:22
  • @Cheddar: See my revisions. I added a PadLeft (using zeroes) to ensure it's always 7 characters. The length check isn't needed in this case. – SnookerC Sep 24 '14 at 14:08