locked
guid RRS feed

  • Question

  • User1080785583 posted

    String.Format("{0}_{1}", "CI", Guid.NewGuid())

    Is there a way to do this with guid to begin with? I would like to always have the first character be a letter.

    Tuesday, September 10, 2013 1:06 PM

Answers

  • User-1360095595 posted

    Perhaps you can create an extension method where you generate a new id then replace the first character of it's a number. Then you can simply call this extension method to get your id.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 10, 2013 1:29 PM
  • User-760709272 posted
    public static class AlphaGuid
    {
        public static Guid NewAlphaGuid()
        {
            Guid g;
    
            do{
                g = Guid.NewGuid();
            } while (!char.IsLetter(g.ToString("D")[0]));
    
            return g;
        }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        Guid myGuid = AlphaGuid.NewAlphaGuid();


    You'll need something like this then

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 10, 2013 4:59 PM

All replies

  • User281315223 posted

    I've always found the String.Format() method to be a very "clean" approach to doing this as opposed to concatenating strings.

    You could use the Guid.ToString() method if you needed to or wanted to use some kind of concatenation as seen below : 

    "CI" + Guid.NewGuid().ToString()

    or 

    "CI_" + Guid.NewGuid().ToString()

    Tuesday, September 10, 2013 1:11 PM
  • User1080785583 posted

    Yes I only use string.format and throw all other concat ideas out the window. The thing is, we must begin with alpha character and guid.newguid by design does not give that functionality to parse first character for alphanumeric regex. I was curious if there was a way to overload guid.newguid ctor to provide this functionality.

    thank you in advance.

    Tuesday, September 10, 2013 1:19 PM
  • User-1360095595 posted

    Perhaps you can create an extension method where you generate a new id then replace the first character of it's a number. Then you can simply call this extension method to get your id.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 10, 2013 1:29 PM
  • User753101303 posted

    Hi,

    A Guid is a number (and is shown as an hexadecimal value) and the fact that this number "starts" with a A to F hexadecimal digit should be irrelevant. You could perhaps call NewGuid until you get a GUID that match your need. My first move would be to look at why this is required when it shouldn't. Note sure to fuklly understand but perhaps (console application test)  :

            static Guid GetAlphaGuid()
            {
                Guid g;
                do {
                    g = Guid.NewGuid();
                } while (Char.IsDigit(g.ToString()[0]));
                return g;
            }
            static void Main(string[] args)
            {
                for (int i = 1; i <= 5; i++)
                {
                    var g = GetAlphaGuid();
                    Console.WriteLine(g);
                }
                Console.ReadKey();
            }
    

     

     

    Tuesday, September 10, 2013 1:40 PM
  • User-760709272 posted

    String.Format("{0}_{1}", "CI", Guid.NewGuid())

    Is there a way to do this with guid to begin with? I would like to always have the first character be a letter.

    You can't do that, best you can do is make your code a bit easier

    public static class AlphaGuid
    {
        public static string NewAlphaGuid()
        {
            return String.Format("{0}_{1}", "CI", Guid.NewGuid().ToString());
        }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        string myGuid = AlphaGuid.NewAlphaGuid();

    Tuesday, September 10, 2013 1:45 PM
  • User1080785583 posted

    would it be more wise to have an extension method in a class library and use the namespace or have it in the website in a helper file?

    guid is the default scenario being implemented across half the board and for me to replace all guids is become a pain. the reason it must begin with a letter is a business requirement from our vendor.

    Tuesday, September 10, 2013 1:51 PM
  • User281315223 posted

    If you are going to be using it widely across your application, then you'll likely want to just create an extension method within a class library and use it throughout your application.

    If these values are being stored in the database currently, you might even consider storing your actual ID (the GUID with the character) as another field, which you could probably write a simple SQL query to actually generate for you.

    UPDATE YourTable
       SET ID = LEFT(Name, 1) + '_' + CONVERT(NVARCHAR(36),GUID)
     WHERE ID IS NULL

    Tuesday, September 10, 2013 2:05 PM
  • User-760709272 posted

    When you say it must begin with a letter, do you mean a letter then a guid

    A_7c09e021-7a48-4519-9af2-23087bf10088

    or do you mean the GUID must begin with a letter, like

    e8d4e465-96cf-4e54-ab91-a7f3abc155bb

    Tuesday, September 10, 2013 2:16 PM
  • User753101303 posted

    Humm... But then you'll get a string not a guid so you would have to update this at all places where it would matter. Not sure what kind of requirement is this but couldn't another option be to keep your guid and just add a prefix when this value is "exposed" to the vendor or whatever ?

    For now I would create a StringGuid class for example with a NewGUID method but IMO what will be challending is using a string where you were using a GUID will be challenging.

    Or perhaps could this class embed a guid and a prefix so that you can go back and forth between a GUID and a GUID based string (or is using a string rather than a guid also mandatory in the db ???)

     

    Tuesday, September 10, 2013 2:21 PM
  • User1080785583 posted

    the data is in an xml document and their engine does not parse attributes values that start with a number. a guid with a string, is no longer a guid, but a string. we use a guid because we like challenges!

    Alas, I feel extension methods will be able to be controlled easier than replacing hundreds of guid.newguid().tostring().

    Tuesday, September 10, 2013 4:30 PM
  • User-760709272 posted
    public static class AlphaGuid
    {
        public static Guid NewAlphaGuid()
        {
            Guid g;
    
            do{
                g = Guid.NewGuid();
            } while (!char.IsLetter(g.ToString("D")[0]));
    
            return g;
        }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        Guid myGuid = AlphaGuid.NewAlphaGuid();


    You'll need something like this then

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 10, 2013 4:59 PM
  • User1080785583 posted

    Thanks everyone. Always good to know where I can go to get solid answers.

    On a side note, the idea of alphaguid was what I initially thought of at the same time of being funny. Why is there not such a thing? :)

    Tuesday, September 10, 2013 7:21 PM