| Start |
|---|
| Formatting a string Use the |
Padding a string to a fixed length |
| 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. 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 Unicode standard. 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 grapheme, too. So in C#, a character is actually a CodePoint. Which means, if you just reverse a valid string like as a sequence of characters, you will get: selbaŕesiM seL As you can see, the accent is on the R character, instead of the e character. You'll need to reverse each GraphemeCluster only. 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.)... |
| 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 This extension method can be used as follows: |
Checking for empty String using String.IsNullOrEmpty() and String.IsNullOrWhiteSpace() |
Trimming Unwanted Characters Off the Start and/or End of Strings. |
| Construct a string from Array The String from Output : String from Output : String from Output : String from Output : |
| Formatting using ToString Usually we are using Integer to formatted string: double to formatted string: Formatting DateTime using ToString |
Convert Decimal Number to Binary,Octal and Hexadecimal Format |
Splitting a String by specific character |
Getting Substrings of a given string
|
Determine whether a string begins with a given sequence Finding a string within a string Using the
|
Joining an array of strings into a new one |
| Getting a char at specific index and enumerating the string You can use the Notice that the return type is You can also use the indexer to iterate through the characters of the string: |
Splitting a String by another string Result: [ "this", "is", "a", "complete", "sentence" ] |
| Replacing a string within a string Using the All the occurrences of the search string are replaced. This method can also be used to remove part of a string, using the |
| Changing the case of characters within a String The
Note: The reason to use the invariant versions of these methods is to prevent producing unexpected culture-specific letters. This is explained here in detail (opens new window). Example: Note that you can choose to specify a specific Culture (opens new window) when converting to lowercase and uppercase by using the String.ToLower(CultureInfo) (opens new window) and String.ToUpper(CultureInfo) (opens new window) methods accordingly. |
| Concatenate an array of strings into a single string The |
| String Concatenation String Concatenation can be done by using the In C# 6 this can be done as follows: |
Read Article
https://develop.horje.com/learn/1434/reference