Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
Hello,

I have different strings like

C#
string s = "11.2.1";
string s1 = "53.12.1";
string s2 = "9.2.11";
string s3 = "1.12.1";


And I want Output like

C#
s = "110201";
s1 = "531201";
s2 = "090211";
s3 = "011201";


Note: String's length is always less than 9 and there is always 2 numbers before and after .(dot).

Please provide me the best and fastest approach.

Thanks
Posted
Comments
BulletVictim 25-Jul-14 0:32am    
Do you receive the strings with the dots in them? or do you need to generate them?
Bhushan Shah1988 25-Jul-14 2:16am    
Yes i receive the string with the dots and i need to generate as shown in output section.

The simplest logic you can refer is as per this method i have displayed below :-

C#
private string GetFormattedString(string strInput)
        {
            string strOutput = string.Empty;
            if (strInput.Contains("."))
            {
                string[] arrInpt = strInput.Split(new char[] { '.' });
                if(arrInpt.Length == 3){
                    if (arrInpt[0].Length == 1) strOutput = strOutput + "0" + arrInpt[0]; else strOutput = strOutput + arrInpt[0];
                    if (arrInpt[1].Length == 1) strOutput = strOutput + "0" + arrInpt[1]; else strOutput = strOutput + arrInpt[1];
                    if (arrInpt[2].Length == 1) strOutput = strOutput + "0" + arrInpt[2]; else strOutput = strOutput + arrInpt[2];
                }
            }

            return strOutput;
        }


We need to validate the string which will be passed as input to this method before passing if it is in correct format or not then it will return the desired string as output.

Hope this will be of help to you.
 
Share this answer
 
v3
Comments
Bhushan Shah1988 25-Jul-14 1:26am    
This is perfect. But I am looking for some optimized code. Because i need to process thousands of strings.
Hi

Check this simplest code to format the string. Hope this will help you to achieve your output.

C#
private string FormatString(string inputString)
        {
            string formattedString = string.Empty;
            try
            {
                string[] arrInput = inputString.Split('.');
                foreach (string value in arrInput)
                {
                    if (value.Length.Equals(1))
                    { formattedString += "0" + value; }
                    else
                    { formattedString += value; }
                }
            }
            catch (Exception ex)
            { }
            return formattedString;
        }
 
Share this answer
 
Comments
Bhushan Shah1988 25-Jul-14 1:26am    
This is perfect. But I am looking for some optimized code. Because i need to process thousands of strings.
Sharmanuj 25-Jul-14 1:30am    
Hi Bhushan,

what you can do on this code, rather than working for single input/output, you can pass an array or collections of input values and loop it through for input range. get the output in same length of array or collection.

This would lead an upper loop and internal loop you can take as given. "foreach" loop.

Regards
Sharmanuj
Please see this :-

C#
private string GetFormattedString(string strInput)
        {
            return strInput.Split(new char[] { '.' })[0].PadLeft(2, '0')
                 + strInput.Split(new char[] { '.' })[1].PadLeft(2, '0')
                 + strInput.Split(new char[] { '.' })[2].PadLeft(2, '0');
        }
 
Share this answer
 
Comments
Bhushan Shah1988 25-Jul-14 3:27am    
I think this solution is good.

Thanks
C#
private string FormatString(string inputString)
        {
                if (inputString.Contains('.'))
{
                string formattedString = string.Empty;
                string[] arrInput = inputString.Split(".");
                foreach (string value in arrInput)
                {
                    formattedString += value.toString("00");
                }
            return formattedString;
        }
}
return inputString
)
 
Share this answer
 
v2
string s = s.Replace(".","");

Repeat same for all variables or alternatively you can create method and pass strings while calling this method.
 
Share this answer
 
Comments
DamithSL 25-Jul-14 0:43am    
s = s.Replace(".","");
SRS(The Coder) 25-Jul-14 0:50am    
what about the 0 append before the single digits ?

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