Skip to content

Instantly share code, notes, and snippets.

@PiN73
Last active March 4, 2018 00:04
Show Gist options
  • Save PiN73/9536107b22c052af5f7f162f9235e8e6 to your computer and use it in GitHub Desktop.
Save PiN73/9536107b22c052af5f7f162f9235e8e6 to your computer and use it in GitHub Desktop.
Get index from address
using System;
namespace GetIndexFromAddress
{
class Program
{
static int? NextDigitIndex(string s, int start)
{
for (int i = start; i < s.Length; ++i)
{
if (char.IsDigit(s[i]))
return i;
if (!char.IsWhiteSpace(s[i]))
return null;
}
return null;
}
static int? GetIndex(ref string address)
{
int indexEnd = -1;
string indexString = "";
for (int digit = 0; digit < 6; ++digit)
{
int? i = NextDigitIndex(address, indexEnd + 1);
if (i == null)
return null;
indexString += address[(int) i];
indexEnd = (int) i;
}
int index = int.Parse(indexString);
address = address.Remove(0, indexEnd + 1).TrimStart(',', ' ');
return index;
}
static void Main()
{
while (true)
{
string address = Console.ReadLine();
int? index = GetIndex(ref address);
Console.WriteLine($"index: '{index}'");
Console.WriteLine($"address: '{address}'");
Console.WriteLine();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment