Checking for empty String using String.IsNullOrEmpty() and String.IsNullOrWhiteSpace()
Concatenate an array of strings into a single string
The System.String.Join method allows to concatenate all elements in a string array, using a specified separator between each element:
Construct a string from Array
The String.Join method will help us to construct a string From array/list of characters or string. This method accepts two parameters. The first one is the delimiter or the separator which will help you to separate each element in the array. And the second parameter is the Array itself.
String from char array:
Output : a,b,c if we change the delimiter as "" then the output will become abc.
String from List of char:
Output : a|b|c
String from List of Strings:
Output : Ram is a boy
String from array of strings:
Output : Ram_is_a_boy
Convert Decimal Number to Binary,Octal and Hexadecimal Format
To convert decimal number to binary format use base 2
To convert decimal number to octal format use base 8
To convert decimal number to hexadecimal format use base 16
Correctly reversing a string
Most times when people have to reverse a string, they do it more or less like this:
However, what these people don't realize is that this is actually wrong.
And I don't mean because of the missing NULL check.
It is actually wrong because a Glyph/GraphemeCluster can consist out of several codepoints (aka. characters).
To see why this is so, we first have to be aware of the fact what the term "character" actually means.
Character is an overloaded term than can mean many things.
A code point is the atomic unit of information. Text is a sequence of
code points. Each code point is a number which is given meaning by the
A grapheme is a sequence of one or more code points that are displayed
as a single, graphical unit that a reader recognizes as a single
element of the writing system. For example, both a and ä are
graphemes, but they may consist of multiple code points (e.g. ä may be
two code points, one for the base character a followed by one for the
diaresis; but there's also an alternative, legacy, single code point
representing this grapheme). Some code points are never part of any
grapheme (e.g. the zero-width non-joiner, or directional overrides).
A glyph is an image, usually stored in a font (which is a collection
of glyphs), used to represent graphemes or parts thereof. Fonts may
compose multiple glyphs into a single representation, for example, if
the above ä is a single code point, a font may chose to render that as
two separate, spatially overlaid glyphs. For OTF, the font's GSUB and
GPOS tables contain substitution and positioning information to make
this work. A font may contain multiple alternative glyphs for the same
So in C#, a character is actually a CodePoint.
Which means, if you just reverse a valid string like Les Misérables, which can look like this
as a sequence of characters, you will get:
As you can see, the accent is on the R character, instead of the e character.
Although string.reverse.reverse will yield the original string if you both times reverse the char array, this kind of reversal is definitely NOT the reverse of the original string.
You'll need to reverse each GraphemeCluster only.
So, if done correctly, you reverse a string like this:
And - oh joy - you'll realize if you do it correctly like this, it will also work for Asian/South-Asian/East-Asian languages (and French/Swedish/Norwegian, etc.)...
Determine whether a string begins with a given sequence
Finding a string within a string
System.String.Contains you can find out if a particular string exists within a string. The method returns a boolean, true if the string exists else false.
Formatting a string
Use the String.Format() method to replace one or more items in the string with the string representation of a specified object:
Formatting using ToString
Usually we are using String.Format method for formatting purpose, the.ToString is usually used for converting other types to string. We can specify the format along with the ToString method while conversion is taking place, So we can avoid an additional Formatting. Let Me Explain how it works with different types;
Integer to formatted string:
double to formatted string:
Formatting DateTime using ToString
Getting a char at specific index and enumerating the string
You can use the Substring method to get any number of characters from a string at any given location. However, if you only want a single character, you can use the string indexer to get a single character at any given index like you do with an array:
Notice that the return type is char, unlike the Substring method which returns a string type.
You can also use the indexer to iterate through the characters of the string:
Getting Substrings of a given string
Substring returns the string up from a given index, or between two indexes (both inclusive).
Getting x characters from the right side of a string
Visual Basic has Left, Right, and Mid functions that returns characters from the Left, Right, and Middle of a string. These methods does not exist in C#, but can be implemented with Substring(). They can be implemented as an extension methods like the following:
This extension method can be used as follows:
Joining an array of strings into a new one
Padding a string to a fixed length
Splitting a String by another string
[ "this", "is", "a", "complete", "sentence" ]
Splitting a String by specific character
String Concatenation can be done by using the System.String.Concat method, or (much easier) using the + operator:
In C# 6 this can be done as follows:
Trimming Unwanted Characters Off the Start and/or End of Strings.
String.TrimStart() and String.TrimEnd()
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0