Home > Enterprise >  How do I create a string with an appended number to retrieve it via a method?
How do I create a string with an appended number to retrieve it via a method?

Time:08-18

My goal is to create usernames that start with a "Z" and end with 000001 - 000048. I would like to store these in a list and then retrieve them to store in an Excel list. My code is buggy (FormatException), and so I am asking for assistance.

public static string GetUserName()
{
    int length = 5;
    int number = 0;
    List<string> userNames = new List<string>();
    for (int i = 1; i < 49; i  )
    {
        number = i;
        string asString = number.ToString("Z"   length); 
        userNames.Add(asString);
    }
    return userNames.ToString();
}

In this section I would like to save the generated user names to the Excel list

var login = new List<string> { GetUserName(), GetUserName(), GetUserName() };

CodePudding user response:

What about changing your output of the function to what you want?

 public static List<string> GetUserName()
        {
            int length = 5;
            
            List<string> userNames = new List<string>();
            
            for (int i = 1; i < 49; i  )
            {            
                string asString = "Z"  i.ToString().PadLeft(length, '0');
                userNames.Add(asString);
            }
            return userNames;
        }

then you can

var login = GetUserName();

CodePudding user response:

You want a number with 6 digits. Your code doesn't work because the ToString() method has a standard numeric format string and a custom one, both which your format doesn't comply. You could use the 0 place holder to format a 6 digit number string and append the Z at the begining:

string asString = "Z"   number.ToString("000000"); 

You could also use string interpolation instead of append:

string asString = $"Z{number.ToString("000000")}"; 

Edit:

If you want a custom number of digits, you could initialize a string with the desired format:

string numberFormat = new string('0', length);

And use like this:

string asString = $"Z{number.ToString(numberFormat)}";

Edit 2:

You can also wrap 'Z' in the format string, as long as it is between quotes or double quotes. Also, number in your code is useless since it's equal to i, so you can simplify with:

string asString = i.ToString("'Z'000000"); 

CodePudding user response:

You'll probably want to look into PadLeft/PadRight. ToString is a powerful tool, but PadLeft is the thing you are looking for I believe.

"Z"   number.ToString().PadLeft(myTotalStringLength, '0').ToString() ;

CodePudding user response:

You can use PadLeft to format the usernames like below :

"Z"   i.ToString().PadLeft(length, '0')

The whole function could be re-written like this:

public static List<string> GetUserName()
{
    int length = 6;
    List<string> userNames = new List<string>();
    for (int i = 1; i < 49; i  )
        userNames.Add("Z"   i.ToString().PadLeft(length, '0'));

    return userNames.ToString();
}

If you notice, the return type of the function is corrected and removed some unnecessary lines. And to get the list of usernames :

var login = GetUserName();
  • Related