0% found this document useful (0 votes)
13 views

Python String Functions

The document discusses various string functions in C# including case conversion, whitespace manipulation, string searching and replacement, string splitting and joining, formatting and alignment, and miscellaneous functions. Some examples of using each function are provided.

Uploaded by

dass
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Python String Functions

The document discusses various string functions in C# including case conversion, whitespace manipulation, string searching and replacement, string splitting and joining, formatting and alignment, and miscellaneous functions. Some examples of using each function are provided.

Uploaded by

dass
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

AXL IT Academy

C#
String Functions
1. Case Conversion:

ToUpper(): Converts all characters to uppercase.


ToLower(): Converts all characters to lowercase.

Eg.:
string text = "AXL Academy";
string uppercase = text.ToUpper(); // Output: "AXL ACADEMY"
string lowercase = text.ToLower(); // Output: "axl academy"

2. Whitespace Manipulation:

Trim(): Removes leading and trailing whitespace.


StartsWith(" "): Checks if the string starts with whitespace.
EndsWith(" "): Checks if the string ends with whitespace.

Eg:
string text = " AXL Academy ";
string trimmed = text.Trim(); // Output: "AXL Academy"
bool startsWithWhitespace = text.StartsWith(" "); // Output: True
bool endsWithWhitespace = text.EndsWith(" "); // Output: True

3. String Searching and Replacement:


IndexOf(): Finds the first occurrence of a substring.
Replace(): Replaces occurrences of a substring with another string.
StartsWith(): Checks if a string starts with a specific substring.
EndsWith(): Checks if a string ends with a specific substring.

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

4. String Splitting and Joining:

Split(): Splits a string into a string array based on a delimiter.


String.Join(): Joins a string array into a single string, using a separator.

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"

5. Formatting and Alignment:

PadLeft(): Left-aligns a string within a specified width.


PadRight(): Right-aligns a string within a specified width.

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

You might also like