Difference between revisions of "Template:Csharp string functions"
esse quam videri
(Created page with '==String Functions in csharp== string.ToUpper(); string.ToLower(); string.Replace(); etc') |
(→String Functions in csharp) |
||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
==String Functions in csharp== | ==String Functions in csharp== | ||
− | string.ToUpper(); | + | To Upper method returns a string that only has upper case letters |
+ | <syntaxhighlight lang="csharp"> | ||
+ | string strName = "jeff"; | ||
+ | Console.WriteLine(strName.ToUpper());</syntaxhighlight> | ||
+ | |||
+ | will write JEFF | ||
+ | |||
+ | <syntaxhighlight lang="csharp"> | ||
+ | string strName = "Jeff"; | ||
+ | Console.WriteLine(strName.ToLower());</syntaxhighlight> | ||
+ | |||
+ | will write jeff | ||
+ | <syntaxhighlight lang="csharp"> | ||
+ | string strName = "jMeyers"; | ||
+ | string strNamej = "john jacob jingleheimer Schmidt"; | ||
+ | |||
+ | strName = strName .Replace("j" , "J"); | ||
+ | strNamej = strNamej.Replace("j" , "J"); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | strName will be "JMeyers" | ||
+ | strName will be "John Jacob Jingleheimer Schmidt" | ||
− | |||
− | |||
etc | etc | ||
+ | |||
+ | ===Make Our Own String function=== | ||
+ | <syntaxhighlight lang="csharp"> | ||
+ | protected static void WriteColorFull(string s) | ||
+ | { | ||
+ | //Save current console color so we can restore it when we are done | ||
+ | //We don't want to leave the console with a random color | ||
+ | ConsoleColor originalConsoleColor = Console.ForegroundColor; | ||
+ | |||
+ | for (int index = 0; index < s.Length; index++) | ||
+ | { | ||
+ | |||
+ | Console.ForegroundColor = mycolors[(mycolors.Length + index) % mycolors.Length]; //Rotate through colors | ||
+ | Console.Write(s[index]); //Write the current letter from the string | ||
+ | |||
+ | } | ||
+ | Console.Write("\n"); | ||
+ | |||
+ | //Restore Console | ||
+ | Console.ForegroundColor = originalConsoleColor; | ||
+ | |||
+ | } | ||
+ | |||
+ | //An array of console colors | ||
+ | private static ConsoleColor[] mycolors = | ||
+ | { | ||
+ | ConsoleColor.Red, | ||
+ | ConsoleColor.Magenta, | ||
+ | ConsoleColor.DarkMagenta, | ||
+ | ConsoleColor.DarkGreen, | ||
+ | ConsoleColor.DarkRed, | ||
+ | ConsoleColor.DarkGray, | ||
+ | ConsoleColor.DarkBlue, | ||
+ | ConsoleColor.Blue, | ||
+ | ConsoleColor.Gray, | ||
+ | ConsoleColor.Green, | ||
+ | ConsoleColor.Yellow, | ||
+ | ConsoleColor.White | ||
+ | }; | ||
+ | </syntaxhighlight> |
Latest revision as of 03:27, 9 February 2016
String Functions in csharp
To Upper method returns a string that only has upper case letters
string strName = "jeff";
Console.WriteLine(strName.ToUpper());
will write JEFF
string strName = "Jeff";
Console.WriteLine(strName.ToLower());
will write jeff
string strName = "jMeyers";
string strNamej = "john jacob jingleheimer Schmidt";
strName = strName .Replace("j" , "J");
strNamej = strNamej.Replace("j" , "J");
strName will be "JMeyers" strName will be "John Jacob Jingleheimer Schmidt"
etc
Make Our Own String function
protected static void WriteColorFull(string s)
{
//Save current console color so we can restore it when we are done
//We don't want to leave the console with a random color
ConsoleColor originalConsoleColor = Console.ForegroundColor;
for (int index = 0; index < s.Length; index++)
{
Console.ForegroundColor = mycolors[(mycolors.Length + index) % mycolors.Length]; //Rotate through colors
Console.Write(s[index]); //Write the current letter from the string
}
Console.Write("\n");
//Restore Console
Console.ForegroundColor = originalConsoleColor;
}
//An array of console colors
private static ConsoleColor[] mycolors =
{
ConsoleColor.Red,
ConsoleColor.Magenta,
ConsoleColor.DarkMagenta,
ConsoleColor.DarkGreen,
ConsoleColor.DarkRed,
ConsoleColor.DarkGray,
ConsoleColor.DarkBlue,
ConsoleColor.Blue,
ConsoleColor.Gray,
ConsoleColor.Green,
ConsoleColor.Yellow,
ConsoleColor.White
};