ConsoleFormatting.cs has 7 methods for formatting output in C# Console applications.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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