0% found this document useful (0 votes)
3 views

C# string functions

The document provides a comprehensive overview of various C# string functions, detailing their descriptions, examples, and outputs. Key functions include Length, ToUpper, ToLower, Substring, Contains, and more, each accompanied by a code snippet demonstrating its usage. This serves as a reference for developers working with string manipulation in C#.

Uploaded by

pankaj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C# string functions

The document provides a comprehensive overview of various C# string functions, detailing their descriptions, examples, and outputs. Key functions include Length, ToUpper, ToLower, Substring, Contains, and more, each accompanied by a code snippet demonstrating its usage. This serves as a reference for developers working with string manipulation in C#.

Uploaded by

pankaj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Confidential - Oracle Restricted

C# string functions

Function Description Example Output

string str = "Hello";


Gets the number of characters in
Length the string. Console.WriteLine(str.Length); 5

string str = "Hello";

ToUpper Converts the string to uppercase. Console.WriteLine(str.ToUpper()); HELLO

string str = "Hello";

ToLower Converts the string to lowercase. Console.WriteLine(str.ToLower()); hello

string str = "Hello";


Extracts part of the string starting
Substring at a given index. Console.WriteLine(str.Substring(1, 3)); ell

string str = "Hello";


Checks if the string contains a
Contains specified substring. Console.WriteLine(str.Contains("el")); True

string str = "Hello";


Finds the index of the first
IndexOf occurrence of a substring/char. Console.WriteLine(str.IndexOf('e')); 1

string str = "Hello, World!";


Finds the index of the last
LastIndexOf occurrence of a character. Console.WriteLine(str.LastIndexOf('o')); 8

string str = "Hello";


Replaces occurrences of a
Replace substring with another substring. Console.WriteLine(str.Replace("e", "a")); Hallo

Splits the string into an array string str = "a,b,c"; string[] parts = str.Split(',');
Split based on a delimiter. Console.WriteLine(parts[0]); a

string str = " Hello ";


Removes leading and trailing
Trim whitespace. Console.WriteLine(str.Trim()); Hello

string str = " Hello";


Removes leading whitespace or
TrimStart specified characters. Console.WriteLine(str.TrimStart()); Hello

string str = "Hello ";


Removes trailing whitespace or
TrimEnd specified characters. Console.WriteLine(str.TrimEnd()); Hello

Confidential - Oracle Restricted


Confidential - Oracle Restricted

C# string functions

Function Description Example Output

string str = "Hello";


Checks if the string starts with a
StartsWith specific substring. Console.WriteLine(str.StartsWith("He")); True

string str = "Hello";


Checks if the string ends with a
EndsWith specific substring. Console.WriteLine(str.EndsWith("lo")); True

Concatenates elements of a string[] words = { "apple", "banana" }; apple,


Join collection with a delimiter. Console.WriteLine(string.Join(", ", words)); banana

Replaces placeholders in a string string result = string.Format("Hello, {0}!", "World"); Hello,


Format with values. Console.WriteLine(result); World!

string str = "Hello";


Inserts a string at a specified Hello
Insert index. Console.WriteLine(str.Insert(5, " World")); World

string str = "Hello";


Deletes characters from a string
Remove starting at a specified index. Console.WriteLine(str.Remove(2)); He

string str = "Hello";

Clone Returns a copy of the string. Console.WriteLine(str.Clone()); Hello

Compares two strings and


Compare returns an integer (-1, 0, 1). Console.WriteLine(string.Compare("apple", "banana")); -1

string str = "Hello";


Compares the current string with
CompareTo another string. Console.WriteLine(str.CompareTo("World")); -1

string result = string.Concat("Hello", " ", "World!"); Hello


Concat Concatenates multiple strings. Console.WriteLine(result); World!

Copies a specified range of string str = "Hello"; char[] arr = new char[5]; str.CopyTo(0, arr, 0, 5);
CopyTo characters into an array. Console.WriteLine(arr); Hello

string str = "123";


Pads the string on the left with
PadLeft spaces or a specified char. Console.WriteLine(str.PadLeft(5, '0')); 00123

string str = "123";


Pads the string on the right with
PadRight spaces or a specified char. Console.WriteLine(str.PadRight(5, '*')); 123**

Confidential - Oracle Restricted


Confidential - Oracle Restricted

C# string functions

Function Description Example Output

Converts the string into a string str = "Hello"; char[] chars = str.ToCharArray();
ToCharArray character array. Console.WriteLine(chars[0]); H

Returns a normalized version of


Normalize the string. string str = "é"; Console.WriteLine(str.Normalize()); é

string str = "";

IsNullOrEmpty Checks if a string is null or empty. Console.WriteLine(string.IsNullOrEmpty(str)); True

string str = " ";


Checks if a string is null, empty,
IsNullOrWhiteSpace or contains only whitespace. Console.WriteLine(string.IsNullOrWhiteSpace(str)); True

Confidential - Oracle Restricted

You might also like