14,735,028 members
See more:
Hello,

I have different strings like

```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

```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
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.

## Solution 2

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

```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.
v3
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.

## Solution 3

Hi

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

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

## Solution 4

```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');
}```
Bhushan Shah1988 25-Jul-14 3:27am

I think this solution is good.

Thanks

## Solution 5

```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
)```
v2

## Solution 1

string s = s.Replace(".","");

Repeat same for all variables or alternatively you can create method and pass strings while calling this method.
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 ?