0

If I have a table , with row with numbers like 70-0002098, lets just call the row, ID

I need the last 4 numbers for all the table rows,

So what I need is something like

foreach(var row in table)
{
  var Id = row.ID(but just the last 4 digits) 
}
  • 1
    if ID is an integer field, id.ToString(); string a = id.substring(id.length-5, 4); – Nyra Nov 18 '14 at 16:20

2 Answers2

1

Not sure what format you want to store it as, or what you want to do with it after, but... Edit: Added an if check for length to avoid index out of bounds condition. Also corrected syntax- SubString() => Substring()

int count = 0;

foreach(var row in table){
    string temp = row.ID.ToString();
    count += (temp.Length > 5)? Convert.ToInt32(temp.Substring(temp.Length-5, 4)) : Convert.ToInt32(temp);     

    }

// But I have no idea what datatype you have for that or what 
// you want to do (count up the integer values or store in an array or something.
// From here you can do whatever you want.
Nyra
  • 859
  • 1
  • 7
  • 27
  • I want to use it as an integer for a variable whiten a while loop – RyeNyeTheComputerScienceGuy Nov 18 '14 at 16:27
  • this will do it for you, if `row.ID` is of type string (I assume it's not as that wouldn't make much sense in a datatable with that column name) then you can ignore converting it to a string. `count` will track those values until you exit the foreach loop and then can be used later on. Edit: Removed mention of `Right()` – Nyra Nov 18 '14 at 16:40
0

Your illustration suggests that the RowID is not currently a number (its got a hyphen in it) so I assume its a string

id.Right(4);

will return the right four characters. It doesn't guarantee they are numbers though. Right is an extension method of string which can be easily written, or copied from this thread Right Function in C#?

Community
  • 1
  • 1
PhillipH
  • 6,182
  • 1
  • 15
  • 25
  • I've never seen the Right() method, what namespace is it in? – reggaeguitar Nov 18 '14 at 16:26
  • It's a String method originating from VB [link](http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.right(v=vs.100).aspx) – Nyra Nov 18 '14 at 16:37
  • Whoops my bad, I've been using this extension method for so long I forgot its not native. Its documented here http://stackoverflow.com/questions/16782786/right-function-in-c – PhillipH Nov 18 '14 at 16:39
  • 1
    I've never tried to use Left|Right in C# but if it won't compile should probably delete this as future searches may lead one to use this incorrectly. – Nyra Nov 18 '14 at 17:06
  • @alykins - I've edited the post to indicate that its an extension method and the source to the code for that trivial extension to the string type. It will compile if the extension method is available and in my opinion provides a clean and clear solution to the problem. – PhillipH Nov 18 '14 at 20:06
  • I thought from MSDN that it would work still too- but I had no comment one way or other (on validity) as I don't ever use that extension- I do think it is cleaner to do that way though (at least for newer coders who might not have indexing down yet for the substring boundaries) – Nyra Nov 18 '14 at 20:08