0

I'm receiving from my app string of numbers, and I need to get them but I don't know them so in the string I have UID: so search it in the string and then I need to take from the string 9 chars after the word "UID:" in the string

tried some and didn't word well

I just removing what I want and not extract it from the string

string id = txt.Substring(0, txt.LastIndexOf("UID:") + 9);

I know the string I need after UID: always have 9 chars the out put I need to get

EXAMPLE:

UID: 994zxfa6q

I don't know what is it but I know its only have 9 chars.

Nathan Werry
  • 876
  • 7
  • 18
tuarek
  • 69
  • 1
  • 8
  • Why don’t you use regex for this? – Shubham Dec 15 '18 at 23:02
  • 1
    Why would he use regex for such a simple implementation? Resulting in people needing to comprehend the problem he is trying to solve, and comprehend regex. That seems like he is adding complexity to code that is unnecessary. – Nathan Werry Dec 15 '18 at 23:13
  • 1
    @NathanWerry People love useless complexities :) – Camilo Terevinto Dec 15 '18 at 23:17
  • @CamiloTerevinto I work on enterprise scale software, so reading other peoples code is my every day, and I am getting thin grey hair because people learn a shiny new thing and over-implement it, just because it works. – Nathan Werry Dec 15 '18 at 23:20
  • 1
    @NathanWerry I've recently started using RegEx but not in my code, only for an extremely fast replace in Notepad++. Hate reading it – Camilo Terevinto Dec 15 '18 at 23:20
  • 1
    @CamiloTerevinto, don't get me wrong. I love RegEx, and I love when it is necessary. But I also love clean code concepts, in which RexEx violates several principals of clean code. I love reading code that reads like a novel, and not someone smashing the number row with the shift key pressed, lol. – Nathan Werry Dec 15 '18 at 23:23
  • Please check my answer at this post. https://stackoverflow.com/a/53798134/4934682 – Caner LENGER Dec 15 '18 at 23:30

5 Answers5

1

You definitely had the right idea. Almost had it.

string id = txt.Substring(txt.LastIndexOf("UID: ") + 5, 9);
Creyke
  • 1,887
  • 2
  • 12
  • 16
1

I prefer not to have constants and length of constants hardcoded separate from each other. You need to have your starting index be the index of the searched string plus the size of the search string, and then your length should be the size of your id.

var uid = "UID: ";
string id = txt.Substring(txt.IndexOf(uid) + uid.Length, 9);
Nathan Werry
  • 876
  • 7
  • 18
0
string GetUID(string input)
{
    const int uidLength = 9;
    const string uidMarker = "UID: ";
    var markerIndex = input.IndexOf(uidMarker);
    if(markerIndex==-1 || markerIndex + uidMarker.Length + uidLength > input.Length)
    {
        throw new ArgumentException("Input does not contain UID", nameof(input));
    }
    return input.Substring(markerIndex + uidMarker.Length, uidLength);
}

If I understand what you want, you can use this code (or something along those lines). Sorry, may have gotten it wrong as I'm far from PC right now. This code assumes that there is only one "UID:" substring in the input string. Also String.IndexOf and String.Substring are nicely documented.

-1

Your code is almost correct, but you have to remember that the first parameter of string.SubString is an index. So, you need to change:

string id = txt.Substring(0, txt.LastIndexOf("UID:") + 9);

to:

string id = txt.Substring(txt.LastIndexOf("UID:") + 4, 9);
Chris Thompson
  • 490
  • 5
  • 17
-2
    String txt = "UID: 994zxfa6q";
    int pFrom = txt.IndexOf("UID:") + 4;
    Console.WriteLine("pFrom = " + pFrom.ToString());
    int pTo = txt.LastIndexOf("UID:") + 14;
    Console.WriteLine("pTo= " + pTo.ToString());
    String result = txt.Substring(pFrom, pTo - pFrom);
    Console.WriteLine("result " + result); 
SSpoke
  • 5,656
  • 10
  • 72
  • 124