Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to get the first 5 characters of a GUID?

Tags:

c#

From what I understand the following code will create a string looking like "xxxx-xxxxx .. etc"

UserUId = Guid.NewGuid().ToString();

What I would like to do is to capture the first five x's. I know I can do this to get the first 4 but how can I skip the "-" and get the first 5, without first putting the Guid into a variable and then using that variable twice with substrings.

UserUId = Guid.NewGuid().ToString().Substring(0, 4);
like image 659
Alan2 Avatar asked Nov 06 '25 18:11

Alan2


1 Answers

You can use the N format specifier to get the guid without any extra formatting:

UserUId = Guid.NewGuid().ToString("N").Substring(0, 5);
like image 65
Sean Avatar answered Nov 09 '25 08:11

Sean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!