开发者

How Convert all to one string?

I need to convert this line in one string, because my method "Disp开发者_StackOverflow社区layMessage" only accept 1 argument, so how can I do this?

_userOptions.DisplayMessage("\nFile Generated: " +
     BinaryWriter.GetBinaryFileName(filePath, Convert.ToInt32(logSelected)) + 
     "\nTime Elapsed:  {0} minute(s) {1} second(s)",
     timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10 + "\n");


It looks like your DisplayMessage method does not allow for the string format. Try putting this entire content (everything inside the parenthesis of DisplayMessage) inside a String.Format() method. That will make it one string and still allow for the multiple parameters you are passing.


Your string indicates that you want to call the static Format method on the String class, like so:

_userOptions.DisplayMessage(string.Format(
    "\nFile Generated: {0}\nTime Elapsed:  {1} minute(s) {2} second(s)\n",
    BinaryWriter.GetBinaryFileName(filePath, Convert.ToInt32(logSelected)),
    timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10));

However, this will give you a problem, as you have more parameters than you have placeholders in the string.

Additionally, given comments regarding the use of "\n" as a new line delimiter, unless you have a specific need for that specific format (and it does not seem that you do, you aren't indicating you are writing to a file, or to something where the data is going to an external system), it's better to use Environment.NewLine, which you can use like so (note, this still does not address the fact you have more parameters than you do placeholders:

_userOptions.DisplayMessage(string.Format(
    "{0}File Generated: {1}{0}Time Elapsed:  {2} minute(s) {3} second(s){0}",
    Environment.NewLine,
    BinaryWriter.GetBinaryFileName(filePath, Convert.ToInt32(logSelected)),
    timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10));


var msg = String.Format("\nFile Generated: {0}\nTime Elapsed: {1} minute(s) {2} second(s)\n", BinaryWriter.GetBinaryFileName(filePath, Convert.ToInt32(logSelected)), timeSpan.Minutes, timeSpan.Seconds);

_userOptions.DisplayMessage(msg);

This should do it...


I guess you want to use String.Format. This takes a string and replace {#} by the argument index.

Example:

String.Format("Hi {0}, welcome to Stack Overflow!", "ale");

You might want to have a look at How should I concatenate strings?


This is more elegant in my opinion:

string message = string.Format("{0}File Generated: {1}{0}Time Elapsed:  {2} minute(s) {3} second(s) {4} milliseconds{0}", 
    "\n", BinaryWriter.GetBinaryFileName(filePath, Convert.ToInt32(logSelected)), timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10);
_userOptions.DisplayMessage(message);

Using Format there's no need to use any + operator on the strings.


I think, you can use a StringBuilder

StringBuilder sb = new StringBuilder();
        sb.Append("\n");
        sb.Append("File Generated: ");
        sb.Append(BinaryWriter.GetBinaryFileName(filePath, Convert.ToInt32(logSelected)));
        sb.Append("\n");
        sb.Append("Time Elapsed: ");
        sb.Append(timeSpan.Minutes);
        sb.Append(" minute(s)");
        sb.Append(timeSpan.Seconds);
        sb.Append(" second(s)");
        sb.Append();
        _userOptions.DisplayMessage(sb.ToString());

but i think, you have some bug: you have 2 parameters but actually is 3


Try this:

string s= String.Format(
    "\nFile Generated: " +
    BinaryWriter.GetBinaryFileName(filePath, Convert.ToInt32(logSelected)) + 
    "\nTime Elapsed:  {0} minute(s) {1} second(s) {2} msec(s)\n",
    timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10);
_userOptions.DisplayMessage(s);


Find below.

string str = String.Format("\n" + "File Generated: " + BinaryWriter.GetBinaryFileName(filePath, Convert.ToInt32(logSelected)) + "\n" + "Time Elapsed: " + " {0} minute(s)" + " {1} second(s)", timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10 + "\n");

_userOptions.DiplayMessage(str);

Hope this helps.


Instead of {0} and {1}, just use your arguments directly:

_userOptions.DisplayMessage("\n" + "File Generated: " + BinaryWriter.GetBinaryFileName(filePath, Convert.ToInt32(logSelected)) + "\n" + "Time Elapsed:" + timeSpan.Minutes + "minute(s)" + timeSpan.Seconds + "second(s)" + timeSpan.Milliseconds / 10 + "\n");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜