String Methods Part 1
Part of the Fundamentals section of Coddy's C# journey — lesson 61 of 69.
C# has a rich set of built-in string methods that allow you to perform various operations on strings. These methods provide functionalities for manipulating, searching, and transforming strings. Here are some commonly used string methods:
Length: Returns the number of characters in the string.Substring(int startIndex): Returns a substring starting from the specified index to the end of the string.Substring(int startIndex, int length): Returns a substring of specified length starting from the specified index.
StartsWith(string value): Checks if the string starts with the specified prefix.EndsWith(string value): Checks if the string ends with the specified suffix.ToUpper(): Converts the string to uppercase.
ToLower(): Converts the string to lowercase.IndexOf(string value): Returns the index of the first occurrence of the specified substring. Returns-1if the substring is not found.
Here's an example that demonstrates some of these methods:
string message = "Hello, World!";
int length = message.Length;
string sub = message.Substring(7);
string sub2 = message.Substring(0, 5);
int index = message.IndexOf("World");
bool starts = message.StartsWith("Hello");
string lower = message.ToLower();
Console.WriteLine("Length: " + length);
Console.WriteLine("Substring: " + sub);
Console.WriteLine("Substring 2: " + sub2);
Console.WriteLine("Index of 'World': " + index);
Console.WriteLine("Starts with 'Hello': " + starts);
Console.WriteLine("Lowercase: " + lower);Challenge
EasyCreate a method named AnalyzeString that takes a string as input and prints the following analysis using string methods:
- The length of the string
- The substring starting from index 7 to the end
- The substring from index 0 up to, but not including, index 5
- Whether the string starts with "Hello"
- The string converted to lowercase
The output should follow this exact format:
Length: [The length]
Substring: [The First substring]
Substring 2: [The Second substring]
Starts with 'Hello': [true or false]
Lowercase: [The string in lowercase]Note: Assume the input string will be long enough for all operations.
Cheat sheet
C# provides built-in string methods for manipulating and analyzing strings:
Length: Returns the number of characters in the stringSubstring(int startIndex): Returns a substring from the specified index to the endSubstring(int startIndex, int length): Returns a substring of specified length from the specified indexStartsWith(string value): Checks if the string starts with the specified prefixEndsWith(string value): Checks if the string ends with the specified suffixToUpper(): Converts the string to uppercaseToLower(): Converts the string to lowercaseIndexOf(string value): Returns the index of the first occurrence of the specified substring
string message = "Hello, World!";
int length = message.Length; // 13
string sub = message.Substring(7); // "World!"
string sub2 = message.Substring(0, 5); // "Hello"
int index = message.IndexOf("World"); // 7
bool starts = message.StartsWith("Hello"); // true
string lower = message.ToLower(); // "hello, world!"Try it yourself
using System;
public class Program {
public static void AnalyzeString(string str) {
// Write your code here
}
public static void Main(string[] args) {
string message = Console.ReadLine();
AnalyzeString(message);
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic Shortcuts5Operators Part 2
Comparison OperatorsLogical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3