0

How can I achieve formatting string to custom format:

int value = 5000;
String.Format("{0:## ###}", value);
value.ToString("##");

but with value as string, without using conversion to number? something like this:

String.Format("{0:## ###}, "5000");

** UPDATE:

I'm trying to create a generic function:

    public string FormatString(string value, string format = "") {
        if (value == null){
            return "";
        }

        return String.Format("{0:" + format + "}", value);
    }

    public bool OtherFunction(id){
          var data = dc.GetData(id);
          ViewBag.DescriptionText = FormatString(data.Description).Replace("\n", "<br />");
          ViewBag.Phone = FormatString(data.Phone, "(##) ####-#####");
          ViewBag.City= FormatString(data.City);
          [...]
    }
5
  • 5
    It's really unclear what you're trying to achieve, I'm afraid. The "## ###" format is specifically for formatting numbers. What would you expect it to do when given "hello world"? Commented Dec 9, 2013 at 15:22
  • You can't, if you need to format a string, you have to write a formatter for said string, which is probably far more complex than simply converting the string into a number. Commented Dec 9, 2013 at 15:24
  • With "Hello World", I will call the function without specifying format.. So It would be just "Hello World".. Commented Dec 9, 2013 at 15:26
  • So, there is not a way without do a "isNumber(value) then convert"? Commented Dec 9, 2013 at 15:27
  • @IanNelson... thinking twice, It's not making sense.. I wanted to format a string number that I have in some variables from DB, but putting the verification for null string and the formatting in a function and just calling the function for every case.. but it all would be avoided if the "string number" would be really a numeric type, which sadly I can't change in the moment.. Commented Dec 9, 2013 at 15:36

2 Answers 2

1

I don't think something like this exists. Like Jon said, this was design for numbers.

If you want just "format" with # you could write simple function, something like this

public string FormatString(string value, string format = "")
{
    if (String.IsNullOrEmpty(value) || String.IsNullOrEmpty(format))
        return value;

    var newValue = new StringBuilder(format);

    for (int i = 0; i < newValue.Length; i++)
    {
        if (newValue[i] == '#')
            if (value.Length > 0)
            {
                newValue[i] = value[0];
                value = value.Substring(1);
            }
            else
            {
                newValue[i] = '0';
            }
    }

    return newValue.ToString();
}

Of course this is very simple one. You will have to check and decide what to do if format is too long (like here: fill with '0') and when he format is too short (here: just 'truncate' rest of value).

But I think you have an idea how to do this.

Somewhere on my disk I have code for something like this: formatting number in special ways/pattern for invoice number. If I will find this, I'll make some post on blog and paste the link

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

1 Comment

Excellent! Since direct string format is not possible, something like that is what I was trying to achieve.
0

"5000" is a string. The only overload available for string.ToString() is the one with an IFormatProvider [1]. While you could actually implement that, you'll probably end up in something similar to int.Parse() which you don't like.

[1] http://msdn.microsoft.com/de-de/library/29dxe1x2(v=vs.110).aspx

Comments

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.