简体   繁体   English

如何将格式化的字符串保存到字符串变量中?

[英]How do I save a formatted string into a string variable?

I am creating a C# method that should 我正在创建一个C#方法
1. take in a string 1.接一串
2. format that string as Currency (no decimal or cents) and return it 2.将该字符串格式化为货币(无小数或美分)并返回

String.Format is great at formatting, but its formatting applies only when printing or outputting the value. String.Format非常String.Format格式化, 但其格式化仅在打印或输出值时适用。 When I step through the code, I can clearly see that the value that is actually saved into the string is not formatted. 当我单步执行代码时,我可以清楚地看到实际保存到字符串中的值未格式化。 But I need it to be formatted. 但是我需要将其格式化。 And that's where your help is needed. 这就是您需要帮助的地方。

In step 1 above, the string that is input will fall into 1 of 3 possible formats. 在上面的步骤1中,输入的字符串将落入3种可能格式中的1种。 (Below, I am using " to designate the beginning and end of strings. The " is not actually part of the string.) (下面,我使用“来指定字符串的开始和结尾。”实际上不是字符串的一部分。)
1. "<5000" 1.“ <5000”
2. "5000-10000" 2.“ 5000-10000”
3. ">10000" 3.“> 10000”

For these 3 examples, the method should output 对于这3个示例,该方法应输出
1. "<$5,000" 1.“ <$ 5,000”
2. "$5,000 - $10,000" 2.“ $ 5,000-$ 10,000”
3. ">$10,000" 3.“> $ 10,000”

Basically, the method should add $ and , where needed (what String.Format does so well). 基本上,该方法应在需要的地方添加$和,( String.Format得很好)。 The rest of the formatting, like adding in <, >, or - is easy. 其余的格式(例如添加<,>或-)很容易。 I can make a method to manually do this, but there's got to be an easier way! 我可以提供一种手动执行此操作的方法,但是必须有一种更简单的方法!

Just to make sure, you are assigning the result of String.Format somewhere, right? 只是为了确保,你分配的结果String.Format地方,对吧?

string result = String.Format("<{0}>", 123);

Strings in .NET are immutable, so functions always create new strings instead of modifying existing ones. .NET中的字符串是不可变的,因此函数总是创建新的字符串,而不是修改现有的字符串。 Also, "printing and outputting" is in no way magical, so it's not possible for a function to behave differently when its result is outputted afterwards. 同样,“打印和输出”绝不是神奇的,因此,在随后输出结果时,函数的行为就不可能不同。

string.Format doesn't modify the string, but returns an entirely new instance. string.Format不会修改字符串,但会返回一个全新的实例。 You can assign it to a variable through: 您可以通过以下方式将其分配给变量:

var newString = string.Format("{0} is my format", oldString);

You can solve your problem with a Regex , in case you only have raw strings and not the values inside of them. 如果您只有原始字符串而不是其中的值,则可以使用Regex解决您的问题。 This applies only to the three examples you brought, but you can adapt it by changing the pattern. 这仅适用于您带来的三个示例,但是您可以通过更改模式来对其进行调整。

EDIT: I've noticed it doesn't apply the commas, but you can try to modify the pattern to match your desired output. 编辑: 我注意到它不应用逗号,但是您可以尝试修改模式以匹配所需的输出。 Now it works as the author requested. 现在它可以按照作者的要求工作。

string[] samples =
{
    "<5000",
    "5000-10000",
    ">10000"
};
var results = samples.
    Select(s => Regex.Replace(s, @"\d+",
        m => Convert.ToInt32(m.Value).ToString("$#,#"))).
    ToArray();

(copied from my comment) (摘自我的评论)

If you already have a string variable, let's call it s , saying: 如果你已经有了一个字符串变量,我们称之为s ,他说:

string.Format( /* something with s in it */ )

will not change s itself. 不会改变s本身。 You might want to reassign, as in 您可能要重新分配,例如

s = string.Format( /* something with s in it */ )

where on the right-hand side the "old" s object is used, and the result of Format is then "saved" to s and becomes the new s . 其中,在右手边的“老” s使用对象,结果Format ,然后“保存”,以s ,并成为新的s

But note that String.Format cannot format a string as currency. 但是请注意, String.Format不能将字符串格式化为货币。 It can format a number (like a decimal or a double ) as a currency, but once you have string, no format like {0:C} or {0:C0} will help change the string output. 它可以将数字 (例如decimaldouble )格式化为货币,但是一旦有了字符串,就没有诸如{0:C}{0:C0}这样的格式可以帮助更改字符串输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM