Tutorial by Examples

string helloWorld = "hello world, how is it going?"; string[] parts1 = helloWorld.Split(','); //parts1: ["hello world", " how is it going?"] string[] parts2 = helloWorld.Split(' '); //parts2: ["hello", "world,", "how", "is&qu...
string helloWorld = "Hello World!"; string world = helloWorld.Substring(6); //world = "World!" string hello = helloWorld.Substring(0,5); // hello = "Hello" Substring returns the string up from a given index, or between two indexes (both inclusive).
string HelloWorld = "Hello World"; HelloWorld.StartsWith("Hello"); // true HelloWorld.StartsWith("Foo"); // false Finding a string within a string Using the System.String.Contains you can find out if a particular string exists within a string. The method returns ...
String.Trim() string x = " Hello World! "; string y = x.Trim(); // "Hello World!" string q = "{(Hi!*"; string r = q.Trim( '(', '*', '{' ); // "Hi!" String.TrimStart() and String.TrimEnd() string q = "{(Hi*"; string r = q.TrimStart( '{...
Use the String.Format() method to replace one or more items in the string with the string representation of a specified object: String.Format("Hello {0} Foo {1}", "World", "Bar") //Hello World Foo Bar
var parts = new[] { "Foo", "Bar", "Fizz", "Buzz"}; var joined = string.Join(", ", parts); //joined = "Foo, Bar, Fizz, Buzz"
string s = "Foo"; string paddedLeft = s.PadLeft(5); // paddedLeft = " Foo" (pads with spaces by default) string paddedRight = s.PadRight(6, '+'); // paddedRight = "Foo+++" string noPadded = s.PadLeft(2); // noPadded = "Foo" (original string...
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...
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 d...
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: public static class StringExtens...
string nullString = null; string emptyString = ""; string whitespaceString = " "; string tabString = "\t"; string newlineString = "\n"; string nonEmptyString = "abc123"; bool result; result = String.IsNullOrEmpty(nullString); ...
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: string s = "hello"; char c = s[1]; //...
To convert decimal number to binary format use base 2 Int32 Number = 15; Console.WriteLine(Convert.ToString(Number, 2)); //OUTPUT : 1111 To convert decimal number to octal format use base 8 int Number = 15; Console.WriteLine(Convert.ToString(Number, 8)); //OUTPUT : 17 To conv...
string str = "this--is--a--complete--sentence"; string[] tokens = str.Split(new[] { "--" }, StringSplitOptions.None); Result: [ "this", "is", "a", "complete", "sentence" ]
Most times when people have to reverse a string, they do it more or less like this: char[] a = s.ToCharArray(); System.Array.Reverse(a); string r = new string(a); 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 ...
Using the System.String.Replace method, you can replace part of a string with another string. string s = "Hello World"; s = s.Replace("World", "Universe"); // s = "Hello Universe" All the occurrences of the search string are replaced. This method can al...
The System.String class supports a number of methods to convert between uppercase and lowercase characters in a string. System.String.ToLowerInvariant is used to return a String object converted to lowercase. System.String.ToUpperInvariant is used to return a String object converted to upper...
The System.String.Join method allows to concatenate all elements in a string array, using a specified separator between each element: string[] words = {"One", "Two", "Three", "Four"}; string singleString = String.Join(",", words); // singleString =...
String Concatenation can be done by using the System.String.Concat method, or (much easier) using the + operator: string first = "Hello "; string second = "World"; string concat = first + second; // concat = "Hello World" concat = String.Concat(first, second); // ...

Page 1 of 1