If I copy " BOB3 27QK DEPM PJ7J T25G SJZI CJA5 BO5O|123456 " and I want to pass it to my text box, and get only the last 6 digits number in my text box, How to do in c#?
Asked
Active
Viewed 327 times
-3
-
In this case, you could use string.split() on the "|" character. Otherwise, you could use .substring and start at .length - 6. – Terry Tyson Apr 26 '21 at 20:36
-
Can you show the codes? – Kun Salim Apr 26 '21 at 20:38
-
1Does this answer your question? [How to get substring from string in c#?](https://stackoverflow.com/questions/5203052/how-to-get-substring-from-string-in-c) – Richard Duerr Apr 26 '21 at 20:51
2 Answers
2
Using .Split would look like this:
string myString = "BOB3 27QK DEPM PJ7J T25G SJZI CJA5 BO5O|123456";
char[] mySplitChars = { '|' };
string[] myArray = myString.Split(mySplitChars);
Console.WriteLine(myArray[1]);
Using .Substring would look like this:
Console.WriteLine(myString.Substring(myString.Length - 6));
The latter is probably preferred because it is shorter and it does not rely on the "|" character being present. The former would be preferred if the "|" is always present but the number of characters at the end can change.

Terry Tyson
- 629
- 8
- 18
0
How are you passing it your text box? If it's just getting the last n characters: refer to this answer

Daniel Oluwadare
- 53
- 5