Click here to Skip to main content
15,887,349 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need a pattern for strings...
I used PadRight but not work well for me. Can you help-me ?

if I put 1 on textBox1 My result: \LOT_0001
if I put 11 on textBox1 My result: \LOT_00011 -< this wrong. I want: \LOT_0011


C#
public static class PatternFileAndFolder
{
    private static string _lote = @"\LOT_";
    private static string _documento = @"\DOC_";

    public static string LotePattern
    {
        get{return _lote.PadRight(8, '0') ;}
    }

    public static string DocumentoPattern
    {
        get { return _documento.PadRight(8, '0'); }
    }
}

textBox2.Text = PatternFileAndFolder.LotePattern + textBox1.Text;
Posted

How about something like:

C#
string _lote = @"\LOT_{0:0000}";
string x = System.String.Format ( _lote , 11 ) ;
 
Share this answer
 
Comments
Maciej Los 7-Oct-14 15:47pm    
Smart solution ;)
+5!
Pedro Brasil 7-Oct-14 15:57pm    
No work for me... When I put value on textbox the result is same for question

Look
private static string _lote = @"\LOT_{0:0000}";
private static string _documento = @"\DOC_";
private static string x = string.Format(_lote, 8);


public static string LotePattern
{
get { return x; }
}

public static string DocumentoPattern
{
get { return _documento.PadRight(8, '0'); }

}
PIEBALDconsult 7-Oct-14 16:00pm    
Won't work with strings. Do you need to support more than numeric values? If not, parse or use a NumericUpDown rather than a TextBox.
Try this:
C#
private static string _lote = @"\LOT_00";

private static string LotePattern(string stringToPad)
{
    return _lote + ((stringToPad.Length == 1) ? "0" : "") + stringToPad;
}
Test:
C#
string test1 = "1";
string test2 = "11";

string result1 = LotePattern(test1);
string result2 = LotePattern(test2);
 
Share this answer
 
Try the below method


C#
private string AddLeadingZeros(int totalLength, string originalNumber)
        {
            string leadingZeros = string.Empty;
            int counter = 0;
            try
            {
                counter = totalLength - originalNumber.Length;
                for (int leadingZeroCount = 0; leadingZeroCount < counter; leadingZeroCount++)
                {
                    leadingZeros += "0";
                }
                originalNumber = leadingZeros + originalNumber;
            }
            catch (Exception ex)
            { LogExceptionDetails(ex); }
            return originalNumber;
        }




In the method you will need to pass two parameters the first one is the total length and the second one is the number. Basing on the difference of lengths it will calculate the number of zero's to be added to the string.

For example let us consider that the total length is "4" and the original string which you are passing is "1". Now calculate the difference that is total length - length of the original string (4 - 1) then it will add three zero's to the original string. If you consider your original value as "01" the it will add two zero's and so on.
 
Share this answer
 
Hi,
Just try following:

C#
int txtVal = 11;
string sz = "\\LOT_" + txtVal.ToString("0000");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900