using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceFormattingStrings
{
classProgram
{
staticvoid Main(string[] args)
{
Console.WriteLine("A: Use String Fields and Properties");
Console.WriteLine("B: Use String Methods");
Console.WriteLine("C: Format Strings");
Console.WriteLine("D: Use the StringBuilder Class");
Console.WriteLine("E: Use DateTime Properties");
Console.WriteLine("F: Use DateTime Methods");
Console.WriteLine("G: Use TimeSpan Properties");
Console.WriteLine("");
Console.WriteLine("Enter an option <X to exit>:");
string s = Console.ReadLine();
switch (s)
{
case"A":
StringFieldProperties();
break;
case"B":
StringMethods();
break;
case"C":
FormattingStrings();
break;
case"D":
StringBuilderClass();
break;
case"E":
DateTimeProperties();
break;
case"F":
DateTimeMethods();
break;
case"G":
TimeSpanProperties();
break;
default:
Console.WriteLine("Invalid selection. Please select from one of the above.");
break;
}
Console.ReadLine();
}
staticvoidStringFieldProperties()
{
stringmyName = "";
if (myName =="")
{
Console.WriteLine("myName = \"\"");
Console.WriteLine();
}
if (myName == String.Empty)
{
Console.WriteLine("myName = String.Empty");
Console.WriteLine();
}
myName = "Robert Green";
Console.WriteLine("There are {0} characters in '{1}'", myName.Length, myName);
Console.WriteLine();
Console.WriteLine("The string is {0}", myName);
Console.WriteLine("The last character in the string is {0}", myName[myName.Length -1]);
Console.WriteLine();
}
staticvoidStringMethods()
{
string author1 = "Ken Getz";
string author2 = "Robert Gren";
//Comparing strings
if (string.Compare(author1, author2) < 0)
{
Console.WriteLine("The string '{0}' is less than the string '{1}'", author1, author2);
}
if (!String.Equals(author1, author2))
{
Console.WriteLine("The string '{0}' does not equal the string '{1}'", author1, author2);
Console.WriteLine();
}
if (author1.CompareTo(author2) < 0)
{
Console.WriteLine("The string '{0}' is less than the string '{1}'", author1, author2);
}
//Searching for strings
if (author1.StartsWith("Ken"))
{
Console.WriteLine("The first author is Ken Getz");
}
if (author1.EndsWith("Getz"))
{
Console.WriteLine("The first author is Ken Getz");
}
if (author2.Contains("Robert"))
{
Console.WriteLine("The second author is Robert Green");
}
if (author2.IndexOf("Green") > 0)
{
Console.WriteLine("The second author is Robert Green");
}
Console.WriteLine(author1.Insert(author1.Length, " is the first author"));
Console.WriteLine(author2.Insert(0, "The second author is "));
Console.WriteLine();
Console.WriteLine("author1 is still '{0}'", author1);
Console.WriteLine("author2 is still '{0}'", author2);
Console.WriteLine(author2.Remove(5));
Console.WriteLine(author2.Remove(3, 3));
Console.WriteLine();
author1 = "The first author is Ken Getz";
author2 = "The second author is Robert Green";
Console.WriteLine(author1.Replace("Ken Getz", "Robert Green"));
Console.WriteLine(author1.Replace(" is", ":"));
Console.WriteLine();
author1 = " Ken Getz "; //4 spaces either side
author2 = " Robert Green "; //4 spaces either side
Console.WriteLine("author1 is '{0}'", author1.Trim());
Console.WriteLine("author2 is '{0}'", author2.TrimStart());
Console.WriteLine("author2 is '{0}'", author2.TrimEnd());
Console.WriteLine();
author1 = "Ken Getz";
author2 = "Robert Green";
Console.WriteLine("author1 is '{0}'", author1.PadLeft(15));
Console.WriteLine("author1 is '{0}'", author1.PadLeft(15, '-'));
Console.WriteLine("author2 is '{0}'", author2.PadRight(15));
Console.WriteLine("author2 is '{0}'", author2.PadLeft(15, '-'));
Console.WriteLine();
Console.WriteLine("author1 is '{0}'", author1.ToUpper());
Console.WriteLine("author2 is '{0}'", author2.ToLower());
Console.WriteLine();
author1 = "The first author is Ken Getz";
author2 = "The second author is Robert Green";
Console.WriteLine(author1.Substring(20));
Console.WriteLine(author1.Substring(20, 1));
Console.WriteLine(author2.IndexOf(':') + 2);
Console.WriteLine(author2.Substring(author2.IndexOf(':') + 2, 3) );
Console.WriteLine();
stringcourseAuthors = ("Ken Getz, Robert Green");
string[] authorList;
char[] separatorList = { ',', '/' };
authorList = courseAuthors.Split(separatorList);
Console.WriteLine("The first author is {0}", authorList[0].Trim());
Console.WriteLine("The second author is {0}", authorList[1].Trim());
}
staticvoidFormattingStrings()
{
inttotalAmount = 121245424;
Console.WriteLine("Currency: {0:C}", totalAmount);
Console.WriteLine("Decimal: {0:D}", totalAmount);
Console.WriteLine("Scientific: {0:E}", totalAmount);
Console.WriteLine("Fixed point: {0:F}", totalAmount);
Console.WriteLine("General: {0:G}", totalAmount);
Console.WriteLine("Number: {0:N}", totalAmount);
Console.WriteLine("Percent: {0:P}", totalAmount);
Console.WriteLine();
Console.WriteLine(String.Format("Currency: {0:C}", totalAmount));
Console.WriteLine(String.Format("Decimal: {0:D}", totalAmount));
Console.WriteLine(String.Format("Scientific: {0:E}", totalAmount));
Console.WriteLine(String.Format("Fixed point: {0:F}", totalAmount));
Console.WriteLine(String.Format("General: {0:G}", totalAmount));
Console.WriteLine(String.Format("Number: {0:N}", totalAmount));
Console.WriteLine(String.Format("Percent: {0:P}", totalAmount));
Console.WriteLine();
Console.WriteLine("Currency Format: " + totalAmount.ToString("C"));
Console.WriteLine("Decimal Format: " + totalAmount.ToString("D"));
Console.WriteLine("Scientific Format: " + totalAmount.ToString("E"));
Console.WriteLine("Fixed Format: " + totalAmount.ToString("F"));
Console.WriteLine("General Format: " + totalAmount.ToString("G"));
Console.WriteLine("Number Format: " + totalAmount.ToString("N"));
Console.WriteLine("Percent Format: " + totalAmount.ToString("P"));
Console.WriteLine();
DateTimenextCentury = newDateTime(2100, 1, 1);
Console.WriteLine("Short date: {0:d}", nextCentury);
Console.WriteLine("Long date: {0:D}", nextCentury);
Console.WriteLine("Short time: {0:t}", nextCentury);
Console.WriteLine("Long time: {0:T}", nextCentury);
Console.WriteLine("Full date / Long time: {0:F}", nextCentury);
Console.WriteLine("Full date / Short time: {0:f}", nextCentury);
Console.WriteLine("General date / Long time: {0:G}", nextCentury);
Console.WriteLine("General date / Short time: {0:g}", nextCentury);
Console.WriteLine("Month day: {0:M}", nextCentury);
Console.WriteLine("Month year: {0:Y}", nextCentury);
Console.WriteLine();
}
staticvoidStringBuilderClass()
{
string author1 = "Ken Getz";
string author2 = "Robert Green";
//This is an inefficient way to build the string
//string authors;
//authors =
//author1 + "wrote chapter 1 " + "\n" +
//author2 + "wrote chapter 2 " + "\n" +
//author2 + "wrote chapter 3 " + "\n" +
//author1 + "wrote chapter 4 " + "\n" +
//author1 + "wrote chapter 5 " + "\n" +
//author2 + "wrote chapter 6 "
//Console.WriteLine(authors);
//This is an efficient way to build a string
StringBuilder authors = newStringBuilder();
authors.Append(author1);
authors.Append(" wrote chapter 1");
authors.AppendLine();
authors.Append(author2);
authors.Append(" wrote chapter 2");
authors.AppendLine();
authors.Append(author2);
authors.Append(" wrote chapter 3");
authors.AppendLine();
authors.Append(author1);
authors.Append(" wrote chapter 4");
authors.Append(author1);
authors.Append(" wrote chapter 5");
authors.AppendLine();
authors.Append(author2);
authors.Append(" wrote chapter 6");
authors.AppendLine();
Console.WriteLine(authors);
authors.Insert(0, "Course authors:" + "\n");
Console.WriteLine(authors);
StringBuildernextChapter = newStringBuilder();
nextChapter.Append(author2);
nextChapter.Append(" wrote chapter 7");
nextChapter.AppendLine();
authors.Insert(authors.Length, nextChapter);
Console.WriteLine(authors);
authors.Replace("Ken Getz", "Ken");
authors.Replace("Robert Green", "Robert");
Console.WriteLine(authors);
authors.Remove(0, 16);
}
staticvoidDateTimeProperties()
{
Console.WriteLine("Today: {0}", DateTime.Today);
Console.WriteLine("Now: {0}", DateTime.Now);
Console.WriteLine("Date: {0}", DateTime.Now.Date);
Console.WriteLine();
Console.WriteLine("Month: {0}", DateTime.Now.Month);
Console.WriteLine();
Console.WriteLine("Day: {0}", DateTime.Now.Day);
Console.WriteLine();
Console.WriteLine("Year: {0}", DateTime.Now.Year);
Console.WriteLine();
Console.WriteLine("DayOfWeek: {0}", DateTime.Now.DayOfWeek);
Console.WriteLine();
Console.WriteLine("DayOfYear: {0}", DateTime.Now.DayOfYear);
Console.WriteLine();
Console.WriteLine("TimeOfDay: {0}", DateTime.Now.TimeOfDay);
Console.WriteLine();
Console.WriteLine("Hour: {0}", DateTime.Now.Hour);
Console.WriteLine();
Console.WriteLine("Minute: {0}", DateTime.Now.Minute);
Console.WriteLine();
Console.WriteLine("Second: {0}", DateTime.Now.Second);
Console.WriteLine();
DateTimenextCentury = newDateTime(2100, 1, 1);
Console.WriteLine("Date: {0}", nextCentury.Date);
Console.WriteLine("DayOfWeek: {0}", nextCentury.DayOfWeek);
Console.WriteLine("Month: {0}", nextCentury.Month);
Console.WriteLine("Day: {0}", nextCentury.Day);
Console.WriteLine("Year: {0}", nextCentury.Year);
}
staticvoidDateTimeMethods()
{
Console.WriteLine("Today: {0}", DateTime.Today.ToShortDateString());
Console.WriteLine("Today: {0}", DateTime.Today.ToLongDateString());
Console.WriteLine("Today: {0}", DateTime.Today.ToShortTimeString());
Console.WriteLine("Today: {0}", DateTime.Today.ToLongTimeString());
Console.WriteLine();
DateTimeotherDateTime;
otherDateTime = DateTime.Today.AddDays(5);
Console.WriteLine("5 days from now: {0}", otherDateTime.ToShortDateString());
otherDateTime = DateTime.Now.AddMonths(-5);
Console.WriteLine("5 months ago: {0}", otherDateTime.ToShortDateString());
otherDateTime = DateTime.Now.AddYears(-10);
Console.WriteLine("10 years ago: {0}", otherDateTime.ToLongDateString());
Console.WriteLine();
otherDateTime = DateTime.Today.AddHours(5);
Console.WriteLine("5 hours from now: {0}", otherDateTime.ToShortTimeString());
otherDateTime = DateTime.Now.AddMinutes(-10);
Console.WriteLine("10 minutes ago: {0}", otherDateTime.ToShortTimeString());
otherDateTime = DateTime.Now.AddSeconds(100);
Console.WriteLine("100 seconds from now: {0}", otherDateTime.ToLongTimeString());
}
staticvoidTimeSpanProperties()
{
DateTimerightNow = DateTime.Now;
DateTimenextMinute = newDateTime(DateTime.Now.Year,
DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.AddMinutes(1).Hour,
DateTime.Now.AddMinutes(1).Minute, 0);
DateTimenextHour = newDateTime(DateTime.Now.Year,
DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.AddMinutes(1).Hour, 0, 0);
DateTimenextDay = newDateTime(DateTime.Now.Year,
DateTime.Now.Month, DateTime.Now.AddDays(1).Day, 0, 0, 0);
Console.WriteLine("Now: {0}", DateTime.Now.ToLongTimeString());
TimeSpantimeRemaining = nextMinute - rightNow;
Console.WriteLine("Seconds left in this minute: {0}",timeRemaining.Seconds);
timeRemaining = nextHour - rightNow;
Console.WriteLine("minutes left in this hour: {0}", timeRemaining.Minutes);
timeRemaining = nextDay - rightNow;
Console.WriteLine("Hours left in this day: {0}", timeRemaining.Hours);
Console.WriteLine();
DateTimefirstDate = DateTime.Today;
DateTimesecondDate = newDateTime(2009, 12, 31);
//TimeSpan remaining
timeRemaining = secondDate - firstDate;
Console.WriteLine("Today: {0}", DateTime.Today.ToShortDateString());
Console.WriteLine("Days left in century: {0:N0}", timeRemaining.TotalDays);
Console.WriteLine("Hours left in century: {0:N0}", timeRemaining.TotalHours);
Console.WriteLine("Seconds left in century: {0:N0}", timeRemaining.TotalSeconds);
}
}
}