C# Language
* you can perform string manipulation using the 'System' namespace, which provides a variety of classes and methods for working with strings.
String Manipulation
String manipulation in C# involves various operations to modify, concatenate, split, search, and format strings. Here's an overview of some common string manipulation operations in C#:
Concatenation:
string firstName = "Ayan"; string lastName = "Sarkar"; // Concatenation using the + operator string fullName = firstName + " " + lastName;
⤏ Combining two or more strings together.
Substring Extraction:
string sentence = "The quick brown fox jumps over the lazy dog"; // Extracting "brown" string substring = sentence.Substring(10, 5);
⤏ Extracting a portion of a string.
Searching:
string sentence = "The quick brown fox jumps over the lazy dog";
// Returns the index of "fox" (12)
int indexOfFox = sentence.IndexOf("fox");
⤏ Finding the position of a substring within a string.
Replacing:
string sentence = "The quick brown fox jumps over the lazy dog";
// Replaces "brown" with "red"
string newSentence = sentence.Replace("brown", "red");
⤏ Replacing occurrences of a substring with another substring.
Splitting:
string sentence = "The quick brown fox jumps over the lazy dog";
// Splits the string into an array of words
string[] words = sentence.Split(' ');
⤏ Breaking a string into substrings based on a delimiter.
Formatting:
string name = "Ayan";
int age = 28;
// String formatted
string fStr = string.Format("Name {0} and {1} years old.", name, age);
// String interpolation
string iStr = $"Name {name} and {age} years old.";
⤏ Formatting strings using placeholders or string interpolation.
Trimming:
string input = " Hello, World! "; // Removes leading and trailing whitespace string trimmedString = input.Trim();
⤏ Removing leading and trailing whitespace from a string.
• These are just some of the basic string manipulation operations in C#. Depending on your specific requirements, you may need to use additional methods and techniques.
• Here's a simple example demonstrating some common string manipulation operations:
using System;
class Program
{
static void Main(string[] args)
{
string str = "Hello, world!";
// Convert to uppercase
string upperCaseString = str.ToUpper();
Console.WriteLine("Uppercase: " + upperCaseString);
// Convert to lowercase
string lowerCaseString = str.ToLower();
Console.WriteLine("Lowercase: " + lowerCaseString);
// Replace part of the string
string replacedString = str.Replace("world", "everyone");
Console.WriteLine("Replaced: " + replacedString);
// Split the string
string[] parts = str.Split(',');
Console.WriteLine("Split:");
foreach (string part in parts)
{
Console.WriteLine(part);
}
// Concatenate strings
string concatString = string.Concat("Goodbye, ", str[7..]);
Console.WriteLine("Concatenated: " + concatString);
// Check if string starts with or ends with a specific substring
bool startsWithHello = str.StartsWith("Hello");
bool endsWithWorld = str.EndsWith("world");
Console.WriteLine("Starts with 'Hello': " + startsWithHello);
Console.WriteLine("Ends with 'world': " + endsWithWorld);
// Check if string contains a specific substring
bool containsHello = str.Contains("Hello");
bool containsGoodbye = str.Contains("Goodbye");
Console.WriteLine("Contains 'Hello': " + containsHello);
Console.WriteLine("Contains 'Goodbye': " + containsGoodbye);
}
}
Uppercase: HELLO, WORLD! Lowercase: hello, world! Replaced: Hello, everyone! Split: Hello world! Concatenated: Goodbye, world! Starts with 'Hello': True Ends with 'world': False Contains 'Hello': True Contains 'Goodbye': False
• This code demonstrates various operations such as converting a string to uppercase or lowercase, replacing substrings, splitting strings into arrays, concatenating strings, and checking for substrings within a string.
What's Next?
We've now entered the finance section on this platform, where you can enhance your financial literacy.