Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
is there a way to change "abcdefghijklmnop" to "abcd-efgh-ijkl-mnop" using string.format() or perhaps linq?
Posted

Instead of trying to be clever, I'd write a simple extension method that is infinitely more usable:
C#
public static string InsertCharacter(this string str, int pos, char ch)
{
    int lastInsert = 0;
    string insertChar = new string(new char[]{ch});
    while (true)
    {
        if (str.Length - lastInsert > pos)
        {
            lastInsert += (lastInsert == 0) ? pos : pos+1;
            str = str.Insert(lastInsert, insertChar);
        }
        else
        {
            break;
        }
    }
    return str;
}

Usage:
C#
string mystring = "abcdefghijklmnop";
mystring = myString.InsertCharacter(4, '-');


I'm old and often don't remember why I do stuff, much less the whats/wheres/whys of code I wrote, so the clever stuff is wasted on me.
 
Share this answer
 
v3
Comments
RaviRanjanKr 28-Nov-11 9:17am    
Nice Answer, My 5+
Your question is too vague. Do you mean you want to insert 3 dashes in a string 16 characters long, or a more general case ?

This should to the trick:

C#
Out= String.Format("{0}-{1}-{2}-{3}", In.Substring(0, 4), In.Substring(4, 4), In.Substring(8, 4), In.Substring(12, 4));
 
Share this answer
 
v2
Comments
Prince Antony G 28-Nov-11 7:04am    
my 5+
Michel [mjbohn] 28-Nov-11 7:36am    
Why do you split with two String.Format()? You can do it with one.
YvesDaoust 28-Nov-11 7:54am    
Just too lazy to use an array
Michel [mjbohn] 28-Nov-11 8:01am    
You could do it like this:
string.Format("{0}-{1}-{2}-{3}", myString.Substring(0, 4), myString.Substring(4, 4), myString.Substring(8, 4), myString.Substring(12,4))

No need to use an array
YvesDaoust 28-Nov-11 9:04am    
Quite right. I was misled both by the code brwoser and by the documentation, which only report overloads with 1 to 3 fields.

Thanks for the tip !
there are another solutions
1)
SQL
string result = myString.Insert(12, "-").Insert(8, "-").Insert(4, "-");

2)
C#
string result = input.Select((c, i) => i > 0 && i % 4 == 0 ? "-" + c : c.ToString())
                .Aggregate((s1, s2) => s1 + s2);
 
Share this answer
 
Comments
Michel [mjbohn] 28-Nov-11 8:10am    
There are for sure a lot of (weird) solutions for this problem ;-)
Just depending on your needs. You should also have a look at 'regular expressions' to solve things like that.
YvesDaoust 28-Nov-11 9:15am    
In terms of readability, your 1) is the coolest. [Except for the necessary reversal trick.]
#realJSOP 28-Nov-11 9:44am    
Not flexible at all.
Rob Philpott 28-Nov-11 11:41am    
2) is one of the most horrid bits of code I've seen in a while. No-one should ever write stuff like that.
Michel [mjbohn] 28-Nov-11 11:57am    
Compared to http://en.wikipedia.org/wiki/Brainfuck it is not that bad ;)
For any lengh of string this is Dynamic code-
C#
private void button4_Click(object sender, EventArgs e)
{
    string str = "abcdefghijklmnop"; //Here u can take from textbox also.
    int i=0;
    int b = 4;
    for ( i = 0; i < str.Length; i++) 
    {                                
        if (i == b)
        {
            int k = 0;   
            if (i == 4)
            {
                str = str.Insert(i, "-");
            }
            else
            {
                str = str.Insert(b, "-");
            }
            k = k + 1;
            b = b + 4 + k;
        }   
        else
        {
        }
    }
    MessageBox.Show(str);
}
 
Share this answer
 
v3
Comments
#realJSOP 28-Nov-11 9:54am    
I formatted your code and fixed the html tags. Beyond that (and like the other two solutions), your code is only flexuible regarding the length of the input string itself, allowing for no other character to be inserted, nor the number of characters between insertions.
Manisha Tambade 28-Nov-11 10:11am    
Thanks for adding html tags.code is flexible for length,we can again modify for Different ip character like we can read from textbox and can place that in insert string.
And the number of characters between insertions by modifying loop variables.
#realJSOP 28-Nov-11 10:29am    
My point is that to make your code "flexibility", you suggest modifying the code, which could possibly introduce errors into code that used to work before being made more flexible to suit a new use case.

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