字符串连接在 C# 中似乎不起作用

发布于 2024-08-16 10:18:58 字数 274 浏览 5 评论 0原文

我不知道以下字符串有什么问题:

"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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

何以心动 2024-08-23 10:18:58

试试这个:

string filename = 
    String.Format(
        "Report({0:dd-MMM-yyyy} to {1:dd-MMM-yyyy})",
         System.DateTime.Now, System.DateTime.Now.AddMonths(-1));

编辑:由于在您的下载框中,您的文件名在第一个空格处被破坏,您可以尝试其中一个:

filename = HttpUtility.UrlEncode(filename); // OR
filename = """" + filename + """";

似乎有些浏览器不能很好地处理空格很好:带空格的文件名在下载时会被截断。请检查是否可以在其他站点下载其他带有空格的文件名。

Try this:

string filename = 
    String.Format(
        "Report({0:dd-MMM-yyyy} to {1:dd-MMM-yyyy})",
         System.DateTime.Now, System.DateTime.Now.AddMonths(-1));

EDIT: Since in your download box you got your filename broken in first whitespace, you could to try ONE of these:

filename = HttpUtility.UrlEncode(filename); // OR
filename = """" + filename + """";

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.

琴流音 2024-08-23 10:18:58

您需要将其分配给某些内容:

string s = "Report(" + System.DateTime.Now.ToString("dd-MMM-yyyy") + " to " + System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy") + ")"

更新:我刚刚看到您对问题的更新。你如何显示字符串?我猜您正在 GUI 中显示它,并且标签太短而无法显示完整的文本。

You need to assign it to something:

string s = "Report(" + System.DateTime.Now.ToString("dd-MMM-yyyy") + " to " + System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy") + ")"

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.

把昨日还给我 2024-08-23 10:18:58

试试这个:

string newstring = 
  string.Format(
                "Report ({0} to {1})", 
                System.DateTime.Now.ToString("dd-MMM-yyyy"), 
                System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy")
               );

Try this:

string newstring = 
  string.Format(
                "Report ({0} to {1})", 
                System.DateTime.Now.ToString("dd-MMM-yyyy"), 
                System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy")
               );
掀纱窥君容 2024-08-23 10:18:58

您将结果分配给什么?如果使用 string.Format 会更容易阅读代码

What are you assigning the result to? It would be easier to read the code if you used string.Format

神经大条 2024-08-23 10:18:58

您没有将连接结果分配给任何内容,因此无法使用它:

string myConcatenated = "Report(" + System.DateTime.Now.ToString("dd-MMM-yyyy") + ")";

You are not assigning the concatenated result to anything, so can't use it:

string myConcatenated = "Report(" + System.DateTime.Now.ToString("dd-MMM-yyyy") + ")";
魔法唧唧 2024-08-23 10:18:58

使用这段代码...

string test = "Report(" + System.DateTime.Now.ToString("dd-MMM-yyyy") + " to " +
                   System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy") + ")";

我看到了以下结果。

Report(29-Dec-2009 to 29-Nov-2009)

该字符串可能稍后被截断。确保在运行此代码后立即设置断点,并检查分配给它的变量的值(在我的例子中进行测试)。

Using this code...

string test = "Report(" + System.DateTime.Now.ToString("dd-MMM-yyyy") + " to " +
                   System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy") + ")";

I saw the following result.

Report(29-Dec-2009 to 29-Nov-2009)

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).

往日情怀 2024-08-23 10:18:58

如果,如您的上一个问题所示,如果您正在使用此值来创建文件,则可能是“to”之前的空格导致了问题。尝试使用:

"Report("
    + System.DateTime.Now.ToString("dd-MMM-yyyy")
    + "To"
    + System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy")
    +  ")"

代替,看看是否可以解决问题。

如果这确实解决了这个问题,您可能需要弄清楚如何引用整个文件名,这样它就不会被视为三个单独的参数,“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:

"Report("
    + System.DateTime.Now.ToString("dd-MMM-yyyy")
    + "To"
    + System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy")
    +  ")"

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 :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文