Skip to content

Instantly share code, notes, and snippets.

@kyrathasoft
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyrathasoft/2292a9719517246786af to your computer and use it in GitHub Desktop.
Save kyrathasoft/2292a9719517246786af to your computer and use it in GitHub Desktop.
ConsoleFormatting.cs has 7 methods for formatting output in C# Console applications.
using System;
namespace com.williambryanmiller.formatting {
public static class ConsoleFormatting {
/* Note: this code is also available on GitHub Gist @
https://gist.github.com/kyrathasoft/2292a9719517246786af */
public static void nThenPrint(int leadingSpaces, string str) {
string temp = "\n";
for (int i = 0; i < leadingSpaces; i++) {
temp += " ";
}
temp += str;
Console.Write(temp);
}
public static void print(string str){
//writes string to Console, just like Console.Write("hello");
Console.Write(str);
}
public static void print(int leadingSpaces, string str) {
//writes string to Console after prepending leading spaces
string temp = string.Empty;
for (int i = 0; i < leadingSpaces; i++) {
temp += " ";
}
temp += str;
Console.Write(temp);
}
public static void print(ConsoleColor before, int leadingSpaces, string p, ConsoleColor after) {
string temp = string.Empty;
for (int i = 0; i < leadingSpaces; i++) {
temp += " ";
}
temp += p;
Console.ForegroundColor = before;
Console.Write(temp);
Console.ForegroundColor = after;
}
public static void print(string str, int trailingSpaces) {
//writes string to Console, appending trailing spaces
string temp = string.Empty;
for (int i = 0; i < trailingSpaces; i++) {
temp += " ";
}
temp = str + temp;
Console.Write(temp);
}
public static void print(int spacesBefore, int spacesAfter, string str) {
//writes string to Console after both prepending and addending spaces to string
string before = string.Empty;
string after = string.Empty;
for (int i = 0; i < spacesBefore; i++) {
before += " ";
}
for (int i = 0; i < spacesAfter; i++) {
after += " ";
}
string temp = before + str + after;
Console.Write(temp);
}
public static void printlines(int numLines) {
for (int i = 1; i <= numLines; i++) {
Console.WriteLine();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment