Python String Functions
Python String Functions
C#
String Functions
1. Case Conversion:
Eg.:
string text = "AXL Academy";
string uppercase = text.ToUpper(); // Output: "AXL ACADEMY"
string lowercase = text.ToLower(); // Output: "axl academy"
2. Whitespace Manipulation:
Eg:
string text = " AXL Academy ";
string trimmed = text.Trim(); // Output: "AXL Academy"
bool startsWithWhitespace = text.StartsWith(" "); // Output: True
bool endsWithWhitespace = text.EndsWith(" "); // Output: True
Eg:
string text = "AXL Academy";
int index = text.IndexOf("Academy"); // Output: 4
string replaced = text.Replace("Academy", "Institute"); // Output: "AXL Institute"
bool startsWithAXL = text.StartsWith("AXL"); // Output: True
bool endsWithAcademy = text.EndsWith("Academy"); // Output: True
Eg.:
string text = "AXL,Academy,Computer,Science";
string[] courses = text.Split(','); // Output: { "AXL", "Academy", "Computer", "Science" }
string joined = String.Join(", ", courses); // Output: "AXL, Academy, Computer, Science"
Eg.:
string text = "AXL Academy";
string paddedLeft = text.PadLeft(20); // Output: " AXL Academy"
string paddedRight = text.PadRight(20); // Output: "AXL Academy "
6. Miscellaneous:
Length: Returns the length of a string (number of characters).
Contains(): Checks if a string contains a specific substring.
Substring(): Extracts a substring from a string.
IsNullOrEmpty(): Checks if a string is null or empty.
GetType() It returns the System.Type of current instance.
GetTypeCode() It returns the Stystem.TypeCode for class System.String.
Remember: C# strings are immutable, so these functions return new strings rather than modifying the original string.
Eg.:
string text = "AXL Academy";
int length = text.Length; // Output: 11
bool containsAXL = text.Contains("AXL"); // Output: True
string substring = text.Substring(0, 3); // Output: "AXL"
bool isNullOrEmpty = String.IsNullOrEmpty(text); // Output: False