我从数据仓库获取社会安全号码(SSN).在将其发布到CRM时,我希望将其格式化为XXX-XX-XXXX而不是XXXXXXXXX.
这就像将一个简单的字符串在位置破折号4和7.我对C#很陌生,那么最好的方法是什么?
class MyString
{
public:
MyString(const std::wstring& s2)
{
s = s2;
}
operator LPCWSTR() const
{
return s.c_str();
}
private:
std::wstring s;
};
int _tmain(int argc, _TCHAR* argv[])
{
MyString s = L"MyString";
CStringW cstring = L"CString";
wprintf(L"%s\n", (LPCWSTR)cstring); // Okay. Becase it has an operator LPCWSTR()
wprintf(L"%s\n", cstring); // Okay, fine. But how?
wprintf(L"%s\n", (LPCWSTR)s); // Okay. fine.
wprintf(L"%s\n", s); // Doesn't work. Why? It prints gabage string like "?."
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何将CString传递给格式字符串%s?
我想将小数转换为字符串,逗号为数千个分隔符,并保留创建小数的相同精度.(将有2-5位有关数字)
decimal d = 1234.4500M;
//I'd like "1,234.4500"
var notRight = d.ToString("###,###.#######"); //1,234.45
var alsoNotRight = d.ToString("###,###.00000");; //1,234.45000
var notRightEither = d.ToString("N"); //1,234.45
var notRightEither2 = d.ToString("G"); //1234.45000
Run Code Online (Sandbox Code Playgroud)
如果不手动解析字符串,是否没有内置方法可以做到这一点?如果没有单一格式字符串,最简单的方法是什么?
我想在我的窗口标题中连接我的viewmodel中的属性和来自资源文件的值.这是我没有来自资源的字符串工作:
Title="Binding Path=Description, StringFormat=Building: {0}}"
Run Code Online (Sandbox Code Playgroud)
现在我想删除"Building"字符串,并从我在其他地方使用的资源中输入一个值:
xmlns:res="clr-namespace:Project.View.Resources"
{res:Strings.TitleDescription}
Run Code Online (Sandbox Code Playgroud)
我怎样才能定义两者?我可以定义为{1}参数吗?
我有一个构造函数,它接受一个DateTime对象:
public Report(DateTime date, string start = "0", string end = "0")
{
Logger.Info("Creating a new Report...");
StartTime = start;
EndTime = end;
Date = date.ToString("YYYY-mm-dd");
SetStartEndTimes();
Logger.Info("Report Created");
}
Run Code Online (Sandbox Code Playgroud)
现在,这只是3天前工作正常.但是,我休息后今天回来,这是我看到的结果:

如您所见,传入的日期是正确的.但是,在格式之后,它不是.再次,这在我休息之前有效.我回来了,我明白了.我错过了什么吗?为什么从开始工作后它的格式如此错误?
编辑
多谢你们.混乱的部分正在查看以前版本的源代码控制,这很有用.或许我想象它有效.我不知道.但这种方式已经持续了大约3个月.
我希望尝试尽可能简单地保持现有字符串的多元化,并且想知道str.format()在寻找kwargs时是否有可能解释默认值.这是一个例子:
string = "{number_of_sheep} sheep {has} run away"
dict_compiled_somewhere_else = {'number_of_sheep' : 4, 'has' : 'have'}
string.format(**dict_compiled_somewhere_else)
# gives "4 sheep have run away"
other_dict = {'number_of_sheep' : 1}
string.format(**other_dict)
# gives a key error: u'has'
# What I'd like is for format to somehow default to the key, or perhaps have some way of defining the 'default' value for the 'has' key
# I'd have liked: "1 sheep has run away"
Run Code Online (Sandbox Code Playgroud)
干杯
我正在尝试使用StringFormat在一个绑定到TextBlock的值周围插入叛逆(撇号?):
<TextBlock Text="{Binding MyValue, StringFormat='The value is '{0}''}"/>
Run Code Online (Sandbox Code Playgroud)
但是,我得到一个编译错误:
MarkupExtension中的名称和值不能包含引号.MarkupExtension参数'MyValue,StringFormat ='值为'{0}''}'无效.
我注意到它确实适用于引号:
<TextBlock Text="{Binding MyValue, StringFormat='The value is "{0}"'}"/>
Run Code Online (Sandbox Code Playgroud)
这是StringFormat的错误吗?
基本上,我想使用.format(),如下所示:
my_string = '{{0}:{1}}'.format('hello', 'bonjour')
Run Code Online (Sandbox Code Playgroud)
并匹配:
my_string = '{hello:bonjour}' #this is a string with literal curly brackets
Run Code Online (Sandbox Code Playgroud)
但是,第一段代码给了我一个错误.
大括号很重要,因为我使用Python通过基于文本的命令与一个软件进行通信.我无法控制fosoftware所期望的格式,因此我最终整理出所有格式是至关重要的.它在字符串周围使用大括号来确保字符串中的空格被解释为单个字符串,而不是多个参数 - 就像通常使用文件路径中的引号一样.
我目前正在使用旧方法:
my_string = '{%s:%s}' % ('hello', 'bonjour')
Run Code Online (Sandbox Code Playgroud)
这当然有效,但.format()似乎更容易阅读,当我在一个字符串中发送包含五个或更多变量的命令时,可读性成为一个重要问题.
谢谢!
String template = "%s and '%'";
String result = String.format(template, "my string");
System.out.println(result);
Run Code Online (Sandbox Code Playgroud)
预期:
my string and '%'
Run Code Online (Sandbox Code Playgroud)
但结果是:
java.util.UnknownFormatConversionException: Conversion = '''
Run Code Online (Sandbox Code Playgroud)
为什么?如何正确地声明序列'%'以使其被忽略String.format()?
为什么下面的代码在'Snakes and Coffee'的评论中被称为'古老的不赞成的方法'到Blender 在python中打印多个参数的帖子?它与Python 2或Python 3的后端代码/实现有关吗?
print("Total score for " + str(name) + " is " + str(score))
Run Code Online (Sandbox Code Playgroud) c# ×4
string ×4
python ×3
.net ×2
wpf ×2
apostrophe ×1
binding ×1
c++ ×1
decimal ×1
escaping ×1
formatting ×1
java ×1
python-2.7 ×1
python-3.x ×1
replace ×1
windows ×1