Im still learning in C#, and there is one thing i cant really seem to find the answer to.
If i have a string that looks like this "abcdefg012345", and i want to make it look like "ab-cde-fg-012345"
i tought of something like this:
string S1 = "abcdefg012345";
string S2 = S1.Insert(2, "-");
string S3 = S2.Insert(6, "-");
string S4 = S3.Insert.....
...
..
Now i was looking if it would be possible to get this al into 1 line somehow, without having to make all those strings.
I assume this would be possible somehow ?
Chaining them like that keeps your line count down. This works because the Insert method returns the string value of s1 with the parameters supplied, then the Insert function is being called on that returned string and so on.
Also it's worth noting that String is a special immutable class so each time you set a value to it, it is being recreated. Also worth noting that String is a special type that allows you to set it to a new instance with calling the constructor on it, the first line above will be under the hood calling the constructor with the text in the speech marks.
You should use a StringBuilder in this case as
Strings objects
areimmutable
and your code would essentially create a completely new string for each one of those operations. http://msdn.microsoft.com/en-us/library/2839d5h5(v=vs.71).aspxSome more information available here: http://www.dotnetperls.com/stringbuilder
Example:
If you really want it on a single line you could simply do something like this:
Just for the sake of completion and to show the use of the lesser known
Aggregate
function, here's another one-liner:result
is ab-cd-ef-g01234-5. I wouldn't recommend this variant, though. It's way too hard to grasp on first sight.Edit: this solution is not valid, anyway, as the "-" will be inserted at the index of the already modified string, not at the positions wrt to the original string. But then again, most of the answers here suffer from the same problem.
Whether or not you can make this a one-liner (you can), it will always cause multiple strings to be created, due to the immutability of the
String
in .NETIf you want to do this somewhat efficiently, without creating multiple strings, you could use a
StringBuilder
. An extension method could also be useful to make it easier to use.Note that this example initialises
StringBuilder
to the correct length up-front, therefore avoiding the need to grow the StringBuilder.Usage:
"abcdefg012345".MultiInsert("-",2,5); // yields "abc-def-g012345"
Live example: http://rextester.com/EZPQ89741