C# Module 2
C# Module 2
CREATING STRINGS :
Example :
string str1 = "Hello, world!";
string str2 = new string(‘XYZ’, 3); // Output “XYZXYZXYZ"
Working With Strings
STRING CONCATENATION :
ACCESSING CHARACTERS :
SUBSTRINGS :
JOINING STRINGS
COMPARING STRINGS :
Example :
double value;
value = 123;
Console.WriteLine(value.ToString("00000"));// Displays 00123
value = 1.2;
Console.WriteLine(value.ToString("0.00“));// Displays 1.20
Console.WriteLine(value.ToString("00.00“));// Displays 01.20
Custom Numeric Format Specifiers
• The “#” custom specifier :
It serves as a digit-placeholder symbol. If the value that is being formatted has a digit
in the position where the "#" symbol appears in the format string, that digit is copied
to the result string. Otherwise, nothing is stored in that position in the result string.
Example:
value = 123456;
Console.WriteLine(value.ToString("[##-##-##]"));
//Displays 12-34-56
Console.WriteLine(value.ToString("(###) ###-####"));
//Displays (123) 456-7890
Custom Numeric Format Specifiers
• The "." custom specifier
It is used to represent the decimal point in a numeric format string. It's
used to specify the location of the decimal point in the formatted output.
Example:
value = 1.2;
Console.WriteLine(value.ToString("0.00”));//Displays 1.20
Console.WriteLine(value.ToString("00.00”));//Displays 01.20
Custom Numeric Format Specifiers
• The "," custom specifier
The "," custom specifier in C# is used as a thousands separator when
formatting numbers. It's commonly used to improve readability by
inserting commas between groups of three digits.
Example :
int number = 1234567;
Console.WriteLine(number.ToString("#,###"));
// Output: 1,234,567
double money = 1234567.89;
Console.WriteLine(money.ToString("#,##0.00"));
// Output: 1,234,567.89
Custom Numeric Format Specifiers
• The "%" custom specifier
A percent sign (%) in a format string causes a number to be
multiplied by 100 before it is formatted. The localized percent
symbol is inserted in the number at the location where the %
appears in the format string.
Example:
double number = 0.75;
Console.WriteLine(number.ToString("0%")); // Output: 75%
Custom Numeric Format Specifiers
• The “0.00” custom specifier: It Formats the number to have two
decimal places, even if they're zeroes.
Example:
double number = 12.3;
Console.WriteLine(number.ToString("0.00")); // Output: 12.30
Example:
DateTime now = DateTime.Now; // Current date and time in local time zone
DateTime utcNow = DateTime.UtcNow; // Current date and time in UTC
DATES AND TIMES
Example:
Example:
string dateString = "2024-04-29";
DateTime parsedDate = DateTime.Parse(dateString); // Parses the string into a DateTime
The output format might vary depending on your system's default date and time
format settings. In this case, it's in the format of MM/dd/yyyy hh:mm:ss.
DATES AND TIMES
Example:
DateTime now = DateTime.Now; //Output = 4/29/2024 1:30:00PM
DateTime tomorrow = now.AddDays(1); // Adds one day, Output=4/30/2024 1:30:00PM
DateTime nextMonth = now.AddMonths(1); // Adds one month
//Output = 5/29/2024 1:30:00PM
TimeSpan difference = nextMonth - now; // Calculates the difference between two dates
//Output=30.00:00:00
DATES AND TIMES
Example:
TimeSpan interval = TimeSpan.FromMinutes(30); // Represents 30 minutes
DateTime futureTime = DateTime.Now.Add(interval); // Adds 30 minutes to current
time
Console.WriteLine("Current Date and Time: " + DateTime.Now);
//Output= 4/29/2024 2:00:00PM
Console.WriteLine("Future Time after 30 minutes: " + futureTime);
///Output= 4/29/2024 2:30:00PM
DATES AND TIMES
Example:
DateTime utcTime = DateTime.UtcNow;
DateTime localTime = utcTime.ToLocalTime();
// Convert UTC time to local time
DateTime anotherTimeZone = utcTime.ToLocalTime().AddHours(2);
// Convert to another time zone
Example:
DateTime now = DateTime.Now;
Console.WriteLine(now.ToString("dd/MM/yyyy")); // Output: 29/04/2024
Console.WriteLine(now.ToString("ddd, MMM dd")); // Output: Mon, Apr 29
Console.WriteLine(now.ToString("yyyy-MM-dd")); // Output: 2024-04-29
Console.WriteLine(now.ToString("HH:mm:ss")); // Output: 13:30:00
Console.WriteLine(now.ToString("hh:mm:ss tt")); // Output: 01:30:00 PM
Converting Strings To Other Types
6.
string strCustom = "42";
int customValue = (int)Convert.ChangeType(strCustom, typeof(int));
Console.WriteLine(customValue); // Output: 42