Coding Schools


 
Python | C Sharp | Azure AI | HTML | JavaScript | CSS | SQL Server
OOPS in C#
C# Data Types
Boxing and Unboxing in C#
Garbage Collection in C#
C# - Conditional Statements
C# - Loops
Interfaces in C#
Generics in C#
Collections in C#
C# 8.0 new features
C# Singleton Design Pattern
C# Factory Design Pattern
LINQ in C#
C# - Program to Find Prime Numbers
C# - Fibonacci Sequence
C# - Factorial of a number
C# - Recursive methods
C# - Anonymous Methods
C# - String Methods
C# TDD - XUnit
C# TDD - NUnit
C# Multiton Design Pattern
C# Facade Design Pattern
C# Abstract Factory Design Pattern
C# Decorator Design Pattern
C# Composite Design Pattern

C# - String Methods



C# provides several methods for string manipulation, which can be quite handy in various programming scenarios. Here are some common string manipulation methods:

1. Concatenation

  • String.Concat or + operator: Combines multiple strings.

csharp
string str1 = "Hello";
string str2 = "World";
string combined = string.Concat(str1, " ", str2); // "Hello World"
// Or using the + operator
string combined = str1 + " " + str2; // "Hello World"

2. Substring

  • Substring(int startIndex) and Substring(int startIndex, int length): Extracts a portion of the string.

csharp
string text = "Hello World";
string substring = text.Substring(0, 5); // "Hello"

3. Replace

  • Replace(string oldValue, string newValue): Replaces occurrences of a specified string in the input string.

csharp
string text = "Hello World";
string replaced = text.Replace("World", "C#"); // "Hello C#"

4. Trim

  • Trim(), TrimStart(), TrimEnd(): Removes whitespace from the beginning and/or end of the string.

csharp
string text = "  Hello World  ";
string trimmed = text.Trim(); // "Hello World"

5. ToUpper and ToLower

  • ToUpper() and ToLower(): Converts all characters in the string to uppercase or lowercase.

csharp
string text = "Hello World";
string upper = text.ToUpper(); // "HELLO WORLD"
string lower = text.ToLower(); // "hello world"

6. Split and Join

  • Split(char separator): Splits the string into an array of substrings based on a delimiter.

  • Join(string separator, string[] value): Combines an array of strings into a single string with a delimiter.

csharp
string text = "Hello,World,from,C#";
string[] words = text.Split(','); // ["Hello", "World", "from", "C#"]
string joined = string.Join(" ", words); // "Hello World from C#"

7. Contains

  • Contains(string value): Checks if the string contains a specified substring.

csharp
string text = "Hello World";
bool containsWorld = text.Contains("World"); // true

8. IndexOf and LastIndexOf

  • IndexOf(char value) and LastIndexOf(char value): Returns the index of the first and last occurrence of a character in the string.

csharp
string text = "Hello World";
int index = text.IndexOf('o'); // 4
int lastIndex = text.LastIndexOf('o'); // 7

9. StartsWith and EndsWith

  • StartsWith(string value) and EndsWith(string value): Checks if the string starts or ends with a specified substring.

csharp
string text = "Hello World";
bool startsWithHello = text.StartsWith("Hello"); // true
bool endsWithWorld = text.EndsWith("World"); // true

10. Format

  • String.Format(format, args): Inserts arguments into a string with placeholders.

csharp
string formatted = string.Format("Name: {0}, Age: {1}", "Alice", 30); // "Name: Alice, Age: 30"

These are just a few examples of the many string manipulation methods available in C#. If you need more details or specific use cases, feel free to ask!




All rights reserved | Privacy Policy | Sitemap