在搜索如何在String.Format中转义单引号时,我在SO处找到答案:在String.Format()中转义单引号
虽然VB似乎有所不同.我测试了它,确实需要C#
string s = DateTime.Now.ToString("MMM d \\'yy 'at' H:mmm");
Run Code Online (Sandbox Code Playgroud)
而VB需要
Dim s As String = Now.ToString("MMM d \'yy 'at' H:mmm")
Run Code Online (Sandbox Code Playgroud)
为什么C#需要双反斜杠,而VB只需要一个反斜杠?这对C#用户来说可能是一个愚蠢的问题,但是虽然我可以阅读C#,但我不习惯写它.
下面的代码产生一个错误,说我的输入字符串格式不正确.为什么?
private void button7_Click(object sender, EventArgs e)
{
string uriAddTagtoGroup =
string.Format("http://localhost:8000/Service/AddTagtoGroup/{group}/{tag}",
textBox6.Text, textBox7.Text);
//line above says input string was not in the correct format?
}
Run Code Online (Sandbox Code Playgroud) 我正在创建一个C#方法,它应该
1.接受一个字符串
2.将该字符串格式化为Currency(无小数或分数)并返回它
String.Format非常适合格式化,但其格式仅适用于打印或输出值.当我单步执行代码时,我可以清楚地看到实际保存到字符串中的值没有格式化.但我需要它格式化.这就是你需要帮助的地方.
在上面的步骤1中,输入的字符串将属于3种可能格式之一.(下面,我用"指定字符串的开头和结尾."实际上并不是字符串的一部分.)
1."<5000"
2."5000-10000"
3."> 10000"
对于这3个例子,该方法应输出
1."<$ 5,000"
2."$ 5,000 - $ 10,000"
3."> $ 10,000"
基本上,该方法应该添加$和,如果需要(什么String.Format做得好).其余的格式化,比如添加<,>或 - 很容易.我可以制作一个方法来手动执行此操作,但必须有一个更简单的方法!
在Java中,这些是相同的:
new File (a.getPath() + filename);
Run Code Online (Sandbox Code Playgroud)
new File (String.format(a.getPath() + filename));
Run Code Online (Sandbox Code Playgroud)
我在findbugs中得到一些警告,第二个选项似乎解决了它.
这让我摸不着头脑.
我有一个函数返回一个包含自动完成控件的类似json数据的字符串,但无法String.Format工作:
private string GetUsers(string crit)
{
string result = "";
using (DataTable dt = dl.GetRecordSet("select ob_id, ob_desc from objects where ac_id = @acid and ob_desc like @crit and ot_id = 29 and ob_deleted = 0 order by ob_desc", new[] { MyApplicationSession.AccountId, "%" + crit + "%" }))
{
result = "[ ";
foreach (DataRow dr in dt.Rows)
{
result += string.Format("{ \"id\": \"{0}\", \"label\": \"{1}\", \"value\": \"{1}\" },", dr["ob_id"].ToString(), dr["ob_desc"].ToString());
}
result = result.Substring(0, result.Length - 1); …Run Code Online (Sandbox Code Playgroud) 我正在使用这段代码获得FormatException
string yyyy = OPENING_DATE.Substring(0, 4);
string mm = OPENING_DATE.Substring(4, 2);
string dd = OPENING_DATE.Substring(6, 2);
ncbaccount.Date_Opened = string.Format("{1}/{2}/{3}",dd,mm,yyyy);
Run Code Online (Sandbox Code Playgroud)
问题:OPENING_DATE就像'20140317'
解决方案:我想要一个像'17/03/2014'这样的字符串
谢谢
对于新行,符号为"%n"; 空间怎么样,有它的象征吗?
我在各种网站上搜索过它,但我找不到它.
在程序中,我想创建一个文本文件(.txt),其文件名包含一个字符串(分配给一个名为的变量FileName).除了文件名中的字符串外,还有一个前缀:"Special File:".在代码中我将前缀写为通常的前缀,但是为了包含变量赋值字符串,我使用.format().
码:
FileName = 'XYZ'
ActiveFile = open('Special File: {0}.txt'.format(FileName), 'a')
Run Code Online (Sandbox Code Playgroud)
这段代码当然有效,但它创建了一个文件而不是文本文件.
如何修复代码以便创建文本文件而不是常规文件File?
我发现每当我使用此方法string.Format()时,它会向我的变量值添加额外的引号和反斜杠.
string value = string.Format("sample {0}", someText);
Run Code Online (Sandbox Code Playgroud)
在VS Watch上,我看到了这个结果:
"sample \"blah\""
Run Code Online (Sandbox Code Playgroud)
在Text Visualizer中,我得到了这个:
sample "blah"
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我为什么我不能得到这个?
"sample blah"
Run Code Online (Sandbox Code Playgroud)
请注意:我已经有一个解决方法,string.Replace("\"","")但我只是想知道添加额外报价的位置.
我正在通过Learn Python the Hard Way练习24,同时将书中使用的所有旧样式格式(%)转换为我喜欢的新样式(.format()).
正如您在下面的代码中看到的,如果我赋值变量"p",我可以成功解包函数返回的元组值.但是当我直接使用该返回值时,它会抛出TypeError.
def secret_formula(started):
jelly_beans = started * 500
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates
start_point = 10000
#Old style
print("We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point))
#New style that works
print("We'd have {p[0]:.0f} beans, {p[1]:.0f} jars, and {p[2]:.0f} crates.".format(p=secret_formula(start_point)))
#This doesn't work:
print("We'd have {0:.0f} beans, {1:.0f} jars, and {2:.0f} crates.".format(secret_formula(start_point)))
Run Code Online (Sandbox Code Playgroud)
引发错误:
Traceback (most recent call last):
File "ex.py", line 16, …Run Code Online (Sandbox Code Playgroud) string.format ×10
c# ×6
java ×2
python ×2
string ×2
.net ×1
currency ×1
escaping ×1
file ×1
formatting ×1
python-3.x ×1
vb.net ×1