I am getting strings like "123456"
, "abcdef"
, "123abc"
from another application.
I need to format the strings like "123-456","abc-def"
, "123-abc"
. The length of the strings can also vary like we can have a string of lenght 30 char. If the 3rd position is chosen then after every 3rd character a hyphen should be inserted.
ex: input "abcdxy123z" output "abc-dxy-123-z" for 3rd position.
if we choose 2nd position then output will be
"ab-cd-xy-12-3z"
I tried with String.Format("{0:####-####-####-####}", Convert.ToInt64("1234567891234567")) but if I get a alphanumeric string, it does not work.
You can make use of String#substring() . String newstring = string. substring(0, 1) + "-" + string. substring(1);
php echo implode("-", str_split("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4)); ?> To format your string, all you need is to replace the alphabets with your string of characters(which could be numeric, alphanumeric or alphabetical), and then replace 4 with the interval in which you want to insert a dash.
You could use a little bit of Linq, like this:
string Hyphenate(string str, int pos) {
return String.Join("-",
str.Select((c, i) => new { c, i })
.GroupBy(x => x.i / pos)
.Select(g => String.Join("", g.Select(x => x.c))));
}
Or like this:
string Hyphenate(string str, int pos) {
return String.Join("-",
Enumerable.Range(0, (str.Length - 1) / pos + 1)
.Select(i => str.Substring(i * pos, Math.Min(str.Length - i * pos, pos))));
}
Or you could use a regular expression, like this:
string Hyphenate(string str, int pos) {
return String.Join("-", Regex.Split(str, @"(.{" + pos + "})")
.Where(s => s.Length > 0));
}
Or like this:
string Hyphenate(string str, int pos) {
return String.Join("-", Regex.Split(str, @"(?<=\G.{" + pos + "})(?!$)"));
}
All of these methods will return the same result:
Console.WriteLine(Hyphenate("abcdxy123z", 2)); // ab-cd-xy-12-3z
Console.WriteLine(Hyphenate("abcdxy123z", 3)); // abc-dxy-123-z
Console.WriteLine(Hyphenate("abcdxy123z", 4)); // abcd-xy12-3z
The power of the for statement:
string str = "12345667889";
int loc = 3;
for(int ins=loc; ins < str.Length; ins+=loc+1)
str = str.Insert(ins,"-");
Gives 123-456-678-89
As a function:
string dashIt(string str,int loc)
{
if (loc < 1) return str;
StringBuilder work = new StringBuilder(str);
for(int ins=loc; ins < work.Length; ins+=loc+1)
work.Insert(ins,"-");
return work.ToString();
}
With StringBuilder:
string input = "12345678"; //could be any length
int position = 3; //could be any other number
StringBuilder sb = new StringBuilder();
for (int i = 0; i < input.Length; i++)
{
if (i % position == 0){
sb.Append('-');
}
sb.Append(input[i]);
}
string formattedString = sb.ToString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With