4

Edit:

Because of your responses I think I've asked the question wrong.

It's not that my solution doesn't work or isn't very clean. I'm interested if there is a general way, how you can foramt a string. Like you can do it with a int or other data types.

So I couldn't find one. But I hope there is one.

So that's the question I wanted to ask:

Does C# provides a way to format strings, like it does for a int or other data types?

I'm looking for something like this:

myString.Format(myFormat);

or:

myFormattedString = String.Format(myString, myFormat);

And if the answer is no, it's also ok. I just want to know it. (And maybe someone else as well)



Original question:

What's the best way to change the format of a string?

So I have a string that looks like this:

"123456789012345678"

And now I want that:

"12.34.567890.12345678"

I'm using this, but I don't find it very clean:

private string FormatString(string myString)
{
    return myString.Insert(2, ".").Insert(5, ".").Insert(12, ".");
}

Things I've tried:

// Too long.
private string FormatString(string myString)
{
    return myString.Substring(0, 2)
         + "."
         + myString.Substring(2, 2)
         + "."
         + myString.Substring(4, 6)
         + "."
         + myString.Substring(10, 8);
}

// Convertion from string -> long -> string.
private string FormatString(string myString)
{
    return String.Format("{0:##'.'##'.'######'.'########}", long.Parse(myString));
}

I'm looking for something like that:

private string FormatString(string myString)
{
    return String.Format("{0:##'.'##'.'######'.'########}", myString);
}
10
  • 6
    "I'm using this, but I don't find it very clean:" I think it´s very clear as it´s easy to understand. Commented Mar 8, 2017 at 9:58
  • Was just typing the same thing as HimBromBeere. This to me is very explicit what is going on. I'd keep it!! Commented Mar 8, 2017 at 9:59
  • Keep what you have. It's really clean. Commented Mar 8, 2017 at 10:01
  • 2
    Possible duplicate of string.insert multiple values. Is this possible? Commented Mar 8, 2017 at 10:13
  • 1
    I would invert the order of Insert: myString.Insert(10, ".").Insert(4, ".").Insert(2, "."). In this way it is more clear what is the initial position where you are inserting the "."... In the way you wrote the code, the second Insert() had to count the already-inserted first "." Commented Mar 8, 2017 at 10:25

3 Answers 3

4

I don't see anything wrong with your code, but if you want a better matching system, you might want to consider regular expressions:

(\d{2})(\d{2})(\d{6})(\d{8})

And replace it with:

$1\.$2\.$3\.$4

(In action)

But my two cents: keep it like it is.

Sign up to request clarification or add additional context in comments.

3 Comments

I'd +2 that link to regex101 if I could..... how come I never about that site? It looks awesome.
This reminds me of a quote by Jamie Zawinski: Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. scnr ;)
Chuckle, I know what you mean! I like to think of myself as Regex expert, but never for longer than a day :)
1

Well...when the framework does not provide what you want, you can always do it yourself.

I've made this method as a experiment. It can surely be optimized and is not fully tested, but it can give you a view of what you could do:

private string FormatString(string myString,string format)
{
    const char number = '#';
    const char character = '%';
    StringBuilder sb = new StringBuilder();

    if (format.Length < myString.Length) throw new Exception("Invalid format string");

    int i = 0;
    foreach (char c in format)
    {
        switch (c)
        {
            case number:
                if (char.IsDigit(myString[i]))
                {
                    sb.Append(myString[i]);
                    i++;
                }
                else
                {
                    throw new Exception("Format string doesn't match input string");
                }
                break;
            case character:
                if (!char.IsDigit(myString[i]))
                {
                    sb.Append(myString[i]);
                    i++;
                }
                else
                {
                    throw new Exception("Format string doesn't match input string");
                }
                break;
            default:
                sb.Append(c);
                break;
        }

    }
    return sb.ToString();
}

This method expects the format string to have either a # to denote digit, a % to denote a character, or any other character that would be copied literally to the formatted string.

Usage:

string test = FormatString("123456789012345678", "##.##.######.########");
//outputs 12.34.567890.12345678
string test = FormatString("12345F789012345678", "##.##.#%####.########");
//outputs 12.34.5F7890.12345678

3 Comments

yeah, this would work, but you know, i'm really lazy... :) But if I interpret your answer correctly, the framework doesn't support a format for a string. That would be really sad. Before c# I worked with Pl/1, where you have this: link
Well i barely remember Cobol also having something similar. I don't think C# has something like this, that btw is a way of validating strings, not a way to format them.Using this method you have something to format and validate the strings. And it's already done, so even being lazy you could use it :)
No it's not only validating. But the reference is a bit shitty. You can see it better hier: link. For alphabetic values it's the same. But I think I'll use my variant with the insert. The next guy who has to understand or change my code, will be grateful. Thank you for your effort, perhaps it helps someone else, who needs formatting and validating.
0

If your string will always be a number then you can do it like this:

string stringData = "123456789012345678";
string dataInFormat = Convert.ToInt64(stringData).ToString(@"##\.##\.######\.########");

First convert string to long and then implement the format on that. In your case it would be like this:

private string FormatString(string myString)
{
    return Convert.ToInt64(myString).ToString(@"##\.##\.######\.########");
}

1 Comment

see: "Things I've tried". I don't like this solution, because you convert the string to a long and then the long back to the string. Expect the Framework is so good and doesn't do this in the background. What I dont think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.