C # Change string format

Edit:

Because of your answers, I think I asked the question incorrectly.

It is not that my solution is not working or not very clean. I am wondering if there is a general way how you can format a string. How can you do this with int or other data types.

So I could not find him. But I hope there is one.

So, the question I wanted to ask:

Does C # provide a way to format strings, for example, for int or other data types?

I am looking for something like this:

myString.Format(myFormat);

or

myFormattedString = String.Format(myString, myFormat);

And if the answer is no, this is also normal. I just want to know that. (And maybe someone else)



The original question:

What is the best way to change the format of a string?

So, I have a line that looks like this:

"123456789012345678"

And now I want:

"12.34.567890.12345678"

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

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

, :

// 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));
}

- :

private string FormatString(string myString)
{
    return String.Format("{0:##'.'##'.'######'.'########}", myString);
}
+4
3

, , :

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

:

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

( )

: , .

+4

... , , .

. , , , , :

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();
}

, #, % , , .

:

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

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

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

First convert the string to a long one, and then insert the format into it. In your case, it will be like this:

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

Source: https://habr.com/ru/post/1671792/


All Articles