Logo

dev-resources.site

for different kinds of informations.

C# {String Methods}

Published at
7/31/2024
Categories
dotnet
dotnetcore
methods
string
Author
firdavs090
Categories
4 categories in total
dotnet
open
dotnetcore
open
methods
open
string
open
Author
10 person written this
firdavs090
open
C# {String Methods}

Length:

1)Length: Gets the number of characters in the string.

string str = "Hello, World!";
int length = str.Length; // 13
Enter fullscreen mode Exit fullscreen mode

Accessing Characters:

1)indexer: Gets the character at a specified index.

char ch = str[0]; // 'H';
Enter fullscreen mode Exit fullscreen mode

Comparison:

1)Equals: Compares two strings for equality.

bool isEqual = str.Equals("Hello, World!"); // true
Enter fullscreen mode Exit fullscreen mode

CompareTo:
1)Compares two strings lexicographically.

int comparison = str.CompareTo("Hello"); // > 0
Enter fullscreen mode Exit fullscreen mode

Compare:
1)Compares two strings with optional culture, case, and sort rules.

int comparison = string.Compare(str, "Hello"); // > 0
Enter fullscreen mode Exit fullscreen mode

Searching:

Contains: Determines whether a substring occurs within the string.
Enter fullscreen mode Exit fullscreen mode
bool contains = str.Contains("World"); // true
Enter fullscreen mode Exit fullscreen mode

StartsWith: Determines whether the string starts with a specified substring.

bool startsWith = str.StartsWith("Hello"); // true
Enter fullscreen mode Exit fullscreen mode

EndsWith: Determines whether the string ends with a specified substring.

bool endsWith = str.EndsWith("World!"); // true
Enter fullscreen mode Exit fullscreen mode

IndexOf: Returns the index of the first occurrence of a specified substring.

int index = str.IndexOf("World"); // 7
Enter fullscreen mode Exit fullscreen mode

LastIndexOf: Returns the index of the last occurrence of a specified substring.

int lastIndex = str.LastIndexOf("o"); // 8
Enter fullscreen mode Exit fullscreen mode

Modification:

Substring: Extracts a substring from the string.

string sub = str.Substring(7, 5); // "World"
Enter fullscreen mode Exit fullscreen mode

Replace: Replaces all occurrences of a specified substring with another substring.

string replaced = str.Replace("World", "C#"); // "Hello, C#!"
Enter fullscreen mode Exit fullscreen mode

Remove: Removes a specified number of characters from the string.

string removed = str.Remove(5, 7); // "Hello!"
Enter fullscreen mode Exit fullscreen mode

Insert: Inserts a specified substring at a specified index.

string inserted = str.Insert(7, "beautiful "); // "Hello, beautiful World!"
Enter fullscreen mode Exit fullscreen mode

Case Conversion:

ToUpper: Converts the string to uppercase.

string upper = str.ToUpper(); // "HELLO, WORLD!"
Enter fullscreen mode Exit fullscreen mode

ToLower: Converts the string to lowercase.

string lower = str.ToLower(); // "hello, world!"
Enter fullscreen mode Exit fullscreen mode

Trimming:

Trim: Removes all leading and trailing white-space characters from the string.

string trimmed = str.Trim();
Enter fullscreen mode Exit fullscreen mode

TrimStart: Removes all leading white-space characters from the string.

string trimmedStart = str.TrimStart();
Enter fullscreen mode Exit fullscreen mode

TrimEnd: Removes all trailing white-space characters from the string.

string trimmedEnd = str.TrimEnd();
Enter fullscreen mode Exit fullscreen mode

Splitting and Joining:

Split: Splits the string into an array of substrings based on a specified delimiter.

string[] words = str.Split(' '); // {"Hello,", "World!"}
Enter fullscreen mode Exit fullscreen mode

Join: Concatenates an array of strings into a single string, with an optional separator.

string joined = string.Join(" ", words); // "Hello, World!"
Enter fullscreen mode Exit fullscreen mode

Format:

Format: Replaces the format items in a string with the string representation of specified objects.

string formatted = string.Format("Welcome, {0}!", "John"); // "Welcome, John!"
Enter fullscreen mode Exit fullscreen mode
string Article's
30 articles in total
Favicon
Leetcode β€” 2942. Find Words Containing Character
Favicon
Python day-28 Dictionary, Frequency of character using nested loops
Favicon
Python Day-19 csv file,String methods,ASCII,Task
Favicon
Redis Cache - A String story
Favicon
Python Day-22 String Functions logic using loops, Recursion, Tasks
Favicon
Greatest Common Divisor of Strings in Javascript
Favicon
Day 22- String Functions and Recursion
Favicon
Day 21- String Functions
Favicon
Python Day-21 String functions logic using loops
Favicon
Python Day-20 String functions logic using loops,Task
Favicon
Day 20 - String functions
Favicon
Day 19 - CSV file, ASCII, String methods
Favicon
Python Day 6- String Functions,Looping-For,ifelse conditions and Task
Favicon
Python Day 5 - String functions
Favicon
Mastering String Operations in JavaScript πŸš€
Favicon
String Data Structures and Algorithms: Essential Interview Questions
Favicon
Find the First Non-Repeated Character in a String
Favicon
Longest substring without repeating characters
Favicon
Create JS function to remove spaces from giving string. ( Using core js and not in-built trim function.)
Favicon
Write a function that removes duplicate characters from a given string. ( Try to write core JS)
Favicon
Knuth Morris Prat algorithm[Pattern Matching]
Favicon
The JS string replace() method
Favicon
Pergunte ao especialista - Strings
Favicon
Strings
Favicon
C# {String Methods}
Favicon
Extending String for Validation in Dart 3
Favicon
String and Trailing comma, get couple and become, Tuple (): A copy & paste mistake to Error and concept
Favicon
Top 10 String Javascript Interview Coding Question
Favicon
Bash string manipulation
Favicon
C++ ηš„ string η‰©δ»Άεˆ°εΊ•δ½”εΉΎε€‹δ½ε…ƒη΅„οΌŸ

Featured ones: