Marco
Marco

Reputation: 19

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

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() };

Upvotes: 1

Views: 87

Answers (4)

Md. Suman Kabir
Md. Suman Kabir

Reputation: 5453

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;
}

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();

EDIT 2

You can use this method which will return 16 user names when it is called :

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

    return userNames;
}

So if you need to call it 3 times, you can do it like below :

List<string> login;
for (int i = 1; i <= 48; i = i + 16)
    login = GetUserName(i);

It's similar to call the function 3 times like this :

 login = GetUserName(1);
 login = GetUserName(17);
 login = GetUserName(33);

Upvotes: 2

Falor
Falor

Reputation: 11

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() ;

Upvotes: 1

Magnetron
Magnetron

Reputation: 8583

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"); 

Upvotes: 1

GeeksGoneBad
GeeksGoneBad

Reputation: 70

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();

Upvotes: 1

Related Questions