Last active
December 7, 2019 20:51
-
-
Save muratbaseren/ae4bbacdd26c9473389fe88ff70fb2e6 to your computer and use it in GitHub Desktop.
String extensions for creating HTML
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
string tipine extension metotlar ekleyerek HTML taglerinin string e eklenmesini sağlayan metotlar. |
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
public static class StringExtensions | |
{ | |
public static string ToHtmlParagraph(this string s) | |
{ | |
return ToHtmlTag(s, "p"); | |
} | |
public static string ToHtmlBold(this string s) | |
{ | |
return ToHtmlTag(s, "b"); | |
} | |
public static string ToHtmlItalic(this string s) | |
{ | |
return ToHtmlTag(s, "i"); | |
} | |
public static string ToHtmlUnderline(this string s) | |
{ | |
return ToHtmlTag(s, "u"); | |
} | |
public static string ToHtmlDivision(this string s) | |
{ | |
return ToHtmlTag(s, "div"); | |
} | |
public static string ToHtmlBreak(this string s) | |
{ | |
return ToHtmlSingleTag("", "br"); | |
} | |
public static string ToHtmlBreak(this string s, int count) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 0; i < count; i++) | |
sb.AppendLine(ToHtmlSingleTag("", "br")); | |
return sb.ToString(); | |
} | |
public static string ToHtmlLine(this string s) | |
{ | |
return ToHtmlSingleTag("", "hr"); | |
} | |
public static string ToHtmlHead(this string s) | |
{ | |
return ToHtmlTag(s, "head"); | |
} | |
public static string ToHtmlBody(this string s) | |
{ | |
return ToHtmlTag(s, "body"); | |
} | |
public static string ToHtml(this string s) | |
{ | |
return ToHtmlTag(s, "html"); | |
} | |
public static string ToHtmlTag(this string s, string tagname) | |
{ | |
return GetTag(tagname, s); | |
} | |
private static string ToHtmlSingleTag(this string s, string tagname) | |
{ | |
return $"<{tagname} />"; | |
} | |
private static string GetTag(string tagname, string val) | |
{ | |
return $"<{tagname}>{val}</{tagname}>"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment