Hi friends,
I have a problem, suppose I enter a number say 123456 then it will give an output as 12-34-56 . How can this possible, Is their any inbuilt function for this is c# language?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Sep 21, 2012, 5:00 AM
FroglegPosted Sep 21, 2012, 4:44 AM
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text))
{
return;
}
string ss = textBox1.Text;
textBox2.Text = StringExtensions.MultiInsert(ss, "-", 1, 3);
}
}
}
public static class StringExtensions
{
public static string MultiInsert(string str, string insertChar, params int[] positions)
{
StringBuilder sb = new StringBuilder(str.Length + (positions.Length * insertChar.Length));
var posLookup = new HashSet
for (int i = 0; i < str.Length; i++)
{
sb.Append(str[i]);
if (posLookup.Contains(i))
sb.Append(insertChar);
}
return sb.ToString();
}