K
K
kevus2018-10-23 16:00:08
Regular Expressions
kevus, 2018-10-23 16:00:08

How to truncate a string from the end in C#?

There is a code:

byte[] ba = Encoding.Default.GetBytes(s);
var hexString  = BitConverter.ToString(ba);
hexString      = hexString.Replace("-", "");
string pattern = @"[A-Z]";
string target  = "";
Regex regex    = new Regex(pattern);
string result  = regex.Replace(hexString, target);
return result;

At the output, I can get something like this:
825052203078204616697461
How to turn
825052203078204616697461
into ?
82505220
Leaving only 8 characters (cut from the end).

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
baimkin, 2018-10-23
@kevus

string str = "Hello Marco !";
if (str.Length > 8)
      str = str.Substring(0, str.Length - (str.Length - 8));

#
#, 2018-10-23
@mindtester

Remove(Int32) upd in your case, just specify the starting position for removal (8)
since string is an immutable class, Remove is also a function that returns a value in which something was removed
ps

stackoweflow
- nope, in this case right away MSDN ;)))
pps more precisely visual studio intellisense, it's useful to view the selection after the dot, just F1, and now MSDN ))

N
netrox, 2018-10-23
@netrox

stackoweflow

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question