字符串连接在 C# 中似乎不起作用
我不知道以下字符串有什么问题:
"Report(" + System.DateTime.Now.ToString("dd-MMM-yyyy") + " to " + System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy") + ")"
我无法获取连接的字符串。我收到了报告(2009 年 12 月 29 日
)。仅此而已 其余部分从字符串中省略。
原因是什么?
I don't know what is wrong with the following string:
"Report(" + System.DateTime.Now.ToString("dd-MMM-yyyy") + " to " + System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy") + ")"
I can't get the concatenated string. I am getting Report(29-Dec-2009
. That's all and
the rest gets left out from the string.
What is the reason?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
试试这个:
编辑:由于在您的下载框中,您的文件名在第一个空格处被破坏,您可以尝试其中一个:
似乎有些浏览器不能很好地处理空格很好:带空格的文件名在下载时会被截断。请检查是否可以在其他站点下载其他带有空格的文件名。
Try this:
EDIT: Since in your download box you got your filename broken in first whitespace, you could to try ONE of these:
Seems some browsers doesn't handles whitespaces very nicely: Filenames with spaces are truncated upon download. Please check it you can to download other filenames with whitespace in other sites.
您需要将其分配给某些内容:
更新:我刚刚看到您对问题的更新。你如何显示字符串?我猜您正在 GUI 中显示它,并且标签太短而无法显示完整的文本。
You need to assign it to something:
Update: I just saw your update to the question. How are you displaying the string? I'm guessing that you are displaying it in a GUI and the label is too short to display the complete text.
试试这个:
Try this:
您将结果分配给什么?如果使用 string.Format 会更容易阅读代码
What are you assigning the result to? It would be easier to read the code if you used string.Format
您没有将连接结果分配给任何内容,因此无法使用它:
You are not assigning the concatenated result to anything, so can't use it:
使用这段代码...
我看到了以下结果。
该字符串可能稍后被截断。确保在运行此代码后立即设置断点,并检查分配给它的变量的值(在我的例子中进行测试)。
Using this code...
I saw the following result.
It could be that the string is being truncated later on. Make sure that you set a breakpoint right after this code is run and check the value of the variable to which it is assigned (test in my case).
如果,如您的上一个问题所示,如果您正在使用此值来创建文件,则可能是“to”之前的空格导致了问题。尝试使用:
代替,看看是否可以解决问题。
如果这确实解决了这个问题,您可能需要弄清楚如何引用整个文件名,这样它就不会被视为三个单独的参数,
“Report(29-Dec-2009 “
、“至”
和“2009 年 11 月 29 日)”
。或者直接将报告名称保留为不含空格。我会选择后者,但我从根本上反对文件名中的空格 - 它们使简单的脚本更难编写:-)
If, as in your previous question, you are using this value to create a file, it may be that it's the space before "to" that is causing the problem. Try to use:
instead and see if that fixes it.
If that does fix it, you'll probably need to either figure out how to quote the entire file name so it's not treated as the three separate arguments,
"Report(29-Dec-2009"
,"to"
and"29-Nov-2009)"
. Or simply leave your reports names without spaces.I'd choose the latter but then I'm fundamentally opposed to spaces in filenames - they make simple scripts so much harder to write :-)