不知道我在这里做错了什么.无法识别扩展方法.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using StringExtensions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
RunTests();
}
static void RunTests()
{
try
{
///SafeFormat
SafeFormat("Hi There");
SafeFormat("test {0}", "value");
SafeFormat("test missing second value {0} - {1}", "test1");
SafeFormat("{0}");
//regular format
RegularFormat("Hi There");
RegularFormat("test {0}", "value");
RegularFormat("test missing second value {0} - {1}", "test1");
RegularFormat("{0}");
///Fails to recognize the extension method here
string.SafeFormat("Hello");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
} …Run Code Online (Sandbox Code Playgroud) 我有一个整数100,我如何格式化它00000100(总是8位数)?
我想打印一个数字列表,但我想在打印之前格式化列表中的每个成员.例如,
theList=[1.343465432, 7.423334343, 6.967997797, 4.5522577]
Run Code Online (Sandbox Code Playgroud)
我希望以上列表作为输入打印以下输出:
[1.34, 7.42, 6.97, 4.55]
Run Code Online (Sandbox Code Playgroud)
对于列表中的任何一个成员,我知道我可以使用它来格式化它
print "%.2f" % member
Run Code Online (Sandbox Code Playgroud)
是否有一个命令/功能可以为整个列表执行此操作?我可以写一个,但想知道是否已经存在.
我有一些列表和更复杂的结构包含浮点数.打印时,我看到有很多十进制数字的浮点数,但是在打印时,我不需要全部.所以我想在打印浮点数时定义自定义格式(例如2或3位小数).
我需要使用浮点数而不是十进制数.此外,我不允许截断/圆形浮动.
有没有办法改变默认行为?
我有一组文本块,我将要展示,我需要以不同的方式显示每个文本块的文本.我目前正在标签属性中保存格式字符串,我需要以这种格式显示文本.如何绑定StringFormat部分?
类似下面的部分:
<TextBlock Tag="{Binding MyFormatString}" Text="{Binding MyProperty, StringFormat='{}{0:MyTag}'}" />
在Java中,如何确定String是否与格式字符串匹配(即:)song%03d.mp3?
换句话说,您将如何实现以下功能?
/**
* @return true if formatted equals String.format(format, something), false otherwise.
**/
boolean matches(String formatted, String format);
Run Code Online (Sandbox Code Playgroud)
例子:
matches("hello world!", "hello %s!"); // true
matches("song001.mp3", "song%03d.mp3"); // true
matches("potato", "song%03d.mp3"); // false
Run Code Online (Sandbox Code Playgroud)
也许有一种方法可以将格式字符串转换为正则表达式?
格式String是一个参数.我不提前知道.song%03d.mp3只是一个例子.它可以是任何其他格式字符串.
如果有帮助,我可以假设格式字符串只有一个参数.
我想将float显示为字符串,同时确保显示至少一个小数位.如果有更多的小数,我希望那些显示.
例如:1应显示为1.0 1.2345应显示为1.2345
有人可以帮我格式化字符串吗?
是否可以使用字符串格式来大写单词?例如,
"{user} did such and such.".format(user="foobar")
Run Code Online (Sandbox Code Playgroud)
应该回归"Foobar做过这样的事情."
请注意,我很清楚.capitalize(); 但是,这是我正在使用的代码(非常简化版本):
printme = random.choice(["On {date}, {user} did la-dee-dah. ",
"{user} did la-dee-dah on {date}. "
])
output = printme.format(user=x,date=y)
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,只是定义user为x.capitalize()在.format()不工作的,因为那么它也将适用(错误地)的第一个场景.由于我无法预测命运,因此无法知道哪些random.choice会提前被选中.我能做什么?
Addt'l note:正好output = random.choice(['xyz'.format(),'lmn'.format()])(换句话说,单独格式化每个字符串,然后.capitalize()用于需要它的那些字符串)不是一个可行的选项,因为printme实际上是从~40 +字符串中选择.
我想使用静态定义的模板进行URL构建.
我正在尝试使用ES6字符串插值功能
var template = "http://example.com/?name=${name}&age=${age}";
var name = "John";
var age = "30";
var url = `${template}`;
Run Code Online (Sandbox Code Playgroud)
预期结果:http://example.com/?name = John" = 23
实际结果:http://example.com/? name = $ {name}&age = $ {age}
在这种情况下,不能用绳子插做的是有没有更好的方法比String.prototype.replace像
var url = template.replace(/\${name}/,"John").replace(/\${age}/, 23);
Run Code Online (Sandbox Code Playgroud) 在Swift中,我可以使用格式说明符格式化String:
// This will return "0.120"
String(format: "%.03f", 0.12)
Run Code Online (Sandbox Code Playgroud)
但官方文档没有提供有关支持的格式说明符的任何信息或链接,或者如何构建类似于以下内容的模板"%.03f":https://developer.apple.com/documentation/swift/string/1417691-init
它只说:
返回使用给定格式字符串作为模板初始化的String对象,其余的参数值将替换为该模板.
string ×4
python ×3
c# ×2
java ×2
.net ×1
binding ×1
ecmascript-6 ×1
javascript ×1
list ×1
python-3.x ×1
regex ×1
swift ×1
wpf ×1