Introduction
String methods help you work with text easily. They let you change, check, or find parts of words or sentences.
Jump into concepts and practice - no test required
String methods help you work with text easily. They let you change, check, or find parts of words or sentences.
string.MethodName(arguments);
MethodName with the name of the string method you want to use.text to uppercase and saves it in upper.string text = "hello";
string upper = text.ToUpper();text contains the word "hello" and stores true or false in hasHello.string text = "hello world"; bool hasHello = text.Contains("hello");
text and saves it in part.string text = "hello world"; string part = text.Substring(0, 5);
text and saves the result in trimmed.string text = " hello ";
string trimmed = text.Trim();This program shows common string methods: changing case, checking content, trimming spaces, and getting a part of the string.
using System; class Program { static void Main() { string greeting = " Hello World! "; string upper = greeting.ToUpper(); string lower = greeting.ToLower(); bool containsWorld = greeting.Contains("World"); string trimmed = greeting.Trim(); string sub = greeting.Substring(2, 5); Console.WriteLine(upper); Console.WriteLine(lower); Console.WriteLine(containsWorld); Console.WriteLine(trimmed); Console.WriteLine(sub); } }
Strings in C# are immutable. Methods return new strings; they do not change the original.
Use Trim() to clean spaces, Contains() to check for text, and Substring() to get parts.
String methods help you work with text easily and safely.
Common methods include ToUpper(), ToLower(), Contains(), Trim(), and Substring().
Remember, these methods return new strings and do not change the original text.
text contains the word "hello"?string s = " Hello World "; string result = s.Trim().Substring(0, 5); Console.WriteLine(result);
string s = "Example";
if(s.Contains("ex"))
{
Console.WriteLine("Found");
}email = "user@example.com". Which code correctly extracts "example" using common string methods?