Difference between revisions of "Template:Csharp string functions"

esse quam videri
Jump to: navigation, search
(String Functions in csharp)
(String Functions in csharp)
Line 3: Line 3:
 
To Upper method returns a string that only has upper case letters
 
To Upper method returns a string that only has upper case letters
 
<csharp>
 
<csharp>
string strName = string.ToUpper("jeff");
+
string strName = "jeff";
</csharp>
+
Console.WriteLine(strName.ToUpper());</csharp>
  
strName will be "JEFF"
+
will write JEFF
  
 
<csharp>
 
<csharp>
string strName = string.ToLower();
+
string strName = "Jeff";
</csharp>
+
Console.WriteLine(strName.ToLower());</csharp>
 
 
strName will be "JEFF"
 
  
 +
will write jeff
 
<csharp>
 
<csharp>
string strName = string.Replace("j" , "J", "jMeyers");
+
string strName = "jMeyers";
string strNamej = string.Replace("j" , "J", "john jacob jingleheimer Schmidt");
+
string strNamej = "john jacob jingleheimer Schmidt";
  
 +
strName = strName .Replace("j" , "J");
 +
strNamej =  strNamej.Replace("j" , "J");
 
</csharp>
 
</csharp>
  
Line 27: Line 28:
 
etc
 
etc
  
 
+
==
 
<csharp>
 
<csharp>
 
protected static void WriteColorFull(string s)
 
protected static void WriteColorFull(string s)

Revision as of 16:08, 23 September 2010

String Functions in csharp

To Upper method returns a string that only has upper case letters <csharp> string strName = "jeff"; Console.WriteLine(strName.ToUpper());</csharp>

will write JEFF

<csharp> string strName = "Jeff"; Console.WriteLine(strName.ToLower());</csharp>

will write jeff <csharp> string strName = "jMeyers"; string strNamej = "john jacob jingleheimer Schmidt";

strName = strName .Replace("j" , "J"); strNamej = strNamej.Replace("j" , "J"); </csharp>

strName will be "JMeyers" strName will be "John Jacob Jingleheimer Schmidt"


etc

== <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
       }; 

</csharp>