如何在控制台应用程序中居中文本? [英] How do I center text in a console application?

查看:667
本文介绍了如何在控制台应用程序中居中文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个控制台应用程序,我需要将文本居中。有没有一种简单的方法来做到这一点,或者我必须在文本之前放置空格,直到它居中?感谢您的帮助。

I am creating a console application and I need to center the text. Is there an easy way to do that, or do I have to place spaces before the text until it is centered? Thank you for your help.

例如,使用|作为控制台的中心:
Hello | World

Example, using '|' as the center of the console: Hello|World

推荐答案

string s = "Hello|World";
Console.SetCursorPosition((Console.WindowWidth - s.Length) / 2, Console.CursorTop);
Console.WriteLine(s);

这篇关于如何在控制台应用程序中居中文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆