我需要以虚线格式格式化我的GUID,全部大写.我知道使用myGuid.ToString("D")或String.Format("{0:D}", myGuid)给出虚线格式,但使用大写D而不是小写d不会给我一个大写的GUID就像我想的那样.有没有办法做到这一点,没有做任何疯狂的事情,或者我只是需要打电话myGuid.ToString().ToUpper()?
为什么这样做可行?:
String f = "Mi name is %s %s.";
System.out.println(String.format(f, "John", "Connor"));
Run Code Online (Sandbox Code Playgroud)
这不是吗?:
String f = "Mi name is %s %s.";
System.out.println(String.format(f, (Object)new String[]{"John","Connor"}));
Run Code Online (Sandbox Code Playgroud)
如果方法String.format采用vararg对象?
它编译好但是当我执行它时,String.format()将vararg对象作为单个唯一参数(数组本身的toString()值),因此它抛出一个MissingFormatArgumentException,因为它无法与第二个字符串说明符匹配(%S).
我怎样才能使它工作?在此先感谢,任何帮助将不胜感激.
我需要将浮点数格式化为x个字符(在我的情况下为6,包括小数点).我的输出还需要包含数字的符号
所以给定输入,这是预期的输出
1.23456 => +1.2345
-12.34567 => -12.345
-0.123456 => -0.1234
1234.567 => +1234.5
Run Code Online (Sandbox Code Playgroud)
请假设在最后一个字符之前总是有一个小数位.即没有 12345.6数字输入 - 输入将始终小于或等于9999.9.
我想这必须有条件地完成.
我想得到一个字符串模板可能在替换中使用的所有可能关键字参数的列表.
有没有办法做到这一点而不是重新?
我想做这样的事情:
text="$one is a $lonely $number."
keys = get_keys(text)
# keys = ('one', 'lonely', 'number')
Run Code Online (Sandbox Code Playgroud)
我正在编写一个简单的类似Mad-lib的程序,我想用string.format或Template字符串执行模板替换.我想写"故事"并让我的程序生成一个模板文件,其中包含用户需要生成的所有"关键字"(名词,动词等).我知道我可以用正则表达式做到这一点,但我想知道是否有替代解决方案?我愿意接受string.format和string template的替代方案.
我认为会有解决方案,但我没有快速搜索过.我确实发现了这个问题,用python反向模板,但它并不是我想要的.它只是重申可以做到这一点re.
编辑:
我应该注意到这$$是'$'的转义,并不是我想要的标记.$$5应该渲染到"5美元".
>>> 'string with no string formatting markers' % ['string']
'string with no string formatting markers'
>>> 'string with no string formatting markers' % ('string',)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
Run Code Online (Sandbox Code Playgroud)
我希望两种情况都能提高TypeError,但事实并非如此.为什么不?
关于这个主题的Python文档讨论了字符串,元组和字典,但没有说明列表.我对这种行为有点困惑.我已经能够在Python 2.7和3.2中复制它.
我有一个字符串,说:
String s = "0123456789";
Run Code Online (Sandbox Code Playgroud)
我想用格式化程序填充它.我可以这两种方式:
String.format("[%1$15s]", s); //returns [ 0123456789]
Run Code Online (Sandbox Code Playgroud)
要么
String.format("[%1$-15s]", s); // returns [0123456789 ]
Run Code Online (Sandbox Code Playgroud)
如果我想截断我做的文字
String.format("[%1$.5s]", s); // returns [01234]
Run Code Online (Sandbox Code Playgroud)
如果我想从左边截断,我想我可以这样做:
String.format("[%1$-.5s]", s); // throws MissingFormatWidthException
Run Code Online (Sandbox Code Playgroud)
但这失败了,所以我尝试了这个:
String.format("[%1$-0.5s]", s); // throws MissingFormatWidthException
Run Code Online (Sandbox Code Playgroud)
以及:
String.format("[%1$.-5s]", s); // throws UnknownFormatConversionException
Run Code Online (Sandbox Code Playgroud)
那么如何使用格式标志从左侧截断?
在Lisp中有没有办法使用命名参数格式化字符串?
也许像关联列表那样的东西
(format t "All for ~(who)a and ~(who)a for all!~%" ((who . "one")))
Run Code Online (Sandbox Code Playgroud)
为了打印"All for one and one for all".
类似于这个python问题,或者这个scala,甚至是c ++,但是在Lisp中.
如果此功能不在语言中,那么是否有人可以使用任何可以完成相同操作的酷函数或宏?
我看到"为什么不join()自动将其参数转换为字符串?" 和接受的答案让我思考:既然
显式优于隐式.
和
错误不应该默默地传递.
为什么str.format()忽略额外/未使用(有时是意外传递)的参数?对我来说,它看起来像是一个无声地传递的错误,它肯定不是明确的:
>>> 'abc'.format(21, 3, 'abc', object(), x=5, y=[1, 2, 3])
'abc'
Run Code Online (Sandbox Code Playgroud)
其实,这导致我的朋友一个问题与os.makedirs(path, exist_ok=True)仍然引发错误,即使该文档os.makedirs()称,exist_ok=True即使不会引发错误path已经存在.原来他只是有一个嵌套函数调用的长行,并且exist_ok被传递到嵌套.format()调用而不是os.makedirs().
为什么下面的代码在'Snakes and Coffee'的评论中被称为'古老的不赞成的方法'到Blender 在python中打印多个参数的帖子?它与Python 2或Python 3的后端代码/实现有关吗?
print("Total score for " + str(name) + " is " + str(score))
Run Code Online (Sandbox Code Playgroud) 我在MongoDB中插入了一个init文件:
db.User.insert({ "_id" : ObjectId("5589929b887dc1fdb501cdba"), "_class" : "com.smartinnotec.aposoft.dao.domain.User", "title" : "DI.", ... "address" : { "_id" : null, ... "country" : "Österreich" }})
Run Code Online (Sandbox Code Playgroud)
如果我用db.User.find()调用此条目,那么我得到以下内容:
{ "_id" : ObjectId("5589929b887dc1fdb501cdba"), "_class" : "com.smartinnotec.aposoft.dao.domain.User", "title" : "DI.", ... "address" : { "_id" : null, ... "country" : "�sterreich" } }
Run Code Online (Sandbox Code Playgroud)
带有特殊字符的单词"�sterreich不正确.
有没有人知道我在mongodb可以做些什么才能解决这个问题?
string-formatting utf special-characters mongodb spring-data
python ×4
java ×2
python-3.x ×2
string ×2
c# ×1
common-lisp ×1
formatting ×1
guid ×1
lisp ×1
mongodb ×1
python-2.7 ×1
spring-data ×1
utf ×1