C# 打印字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14036276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:29:16  来源:igfitidea点击:

Printing Strings

c#stringprinting

提问by compprog254

I am working on a program in which I need to print out a string, I have the string ready, I just need to know how to print, I've never done it so i don't know where to start any ideas? this is what I have so far but I don't know where to go from here

我正在开发一个程序,我需要在其中打印一个字符串,我准备好了字符串,我只需要知道如何打印,我从来没有做过,所以我不知道从哪里开始任何想法?这是我到目前为止所拥有的,但我不知道从哪里开始

PrintDocument output = new PrintDocument();
output.DocumentName = "Test Results";

回答by C Johnson

Your definition of 'Print' is pretty vague. The most common meaning of print in this part of the internet is to print or display text to the console or command window.

您对“打印”的定义非常模糊。在 Internet 的这一部分中,print 最常见的含义是将文本打印或显示到控制台或命令窗口。

So, if you want to print to a console window you use methods on the System.Console class:

因此,如果您想打印到控制台窗口,请使用 System.Console 类上的方法:

http://msdn.microsoft.com/en-us/library/system.console.writeline.aspx

http://msdn.microsoft.com/en-us/library/system.console.writeline.aspx

For example:

例如:

String yourname = "Mr. Nice";
Console.WriteLine("Hello {0}", yourname);

would display:

会显示:

Hello Mr. Nice

你好尼斯先生

回答by kazem

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace Test
{
public class Print
{
    PrintDocument myPrintDocument = new PrintDocument();

    public void ShowPrintDialog()
    {
        PrintDialog myDialog = new PrintDialog();
        myDialog.Document = myPrintDocument;
        if(myDialog.ShowDialog() == DialogResult.OK)
            Print();
    }

    protected override void OnPrintPage(PrintPageEventArgs e)
    {
        e.Graphics.DrawString("Your String To Print In Document", this.Font, Brushes.Red, new PointF());
        e.HasMorePages = false;
    }
}
}

回答by magna_nz

You can print with Console.WriteLine("text")to standard output.

您可以打印Console.WriteLine("text")到标准输出。

Have a look at string interpolation to make adding variables to string far easier. This was introduced in C# 6 and has following syntax.

看看字符串插值,可以更容易地向字符串添加变量。这是在 C# 6 中引入的,具有以下语法。

Console.Writeline($"I am {cool}");where coolis the variable name :)

Console.Writeline($"I am {cool}");cool变量名在哪里:)