Saturday, 20 August 2016

Most commonly used String function in C#

In this article, we are going to learn most commonly used string function in C#. I have compiled some of the most used C# functions below.

1) IndexOf(char)

  1. IndexOf(char) method returns the index position of first occurrence of supplied character.
  2. Index numbering starts from zero.
  3. Returns -1 if character is not found.

Example:

string charString = "this is IndexOf string function.";
int index2 = charString.IndexOf('t');
int index1 = charString.IndexOf('i');
int index3 = charString.IndexOf('y');
Output:
index1 : 2
index2 : 0            
index3 : -1  

2) IndexOf(Char,Int32)

  1. IndexOf(Char, Int32) method returns the index position of first occurrence of supplied character.
  2. Search starts from specified startIndex position and continues until the last character position.
  3. Index numbering starts from zero.
  4. Returns -1 if character is not found.

Example:

string charString = "this is IndexOf Char, Int32 string function.";
int index1 = charString.IndexOf('i',3);
int index2 = charString.IndexOf('z',1);
int index3 = charString.IndexOf('i',44); 
Output:
index1 : 5
index2 : -1           
index3 : -1  

3) LastIndexOf(char)

  1. LastIndexOf(char) method returns the index position of last occurrence supplied character.
  2. Index numbering starts from zero.
  3. Returns -1 if character is not found.

Example:

string charString = "this is LastIndexOf string function.";
int index1 = charString.LastIndexOf('i');
int index2 = charString.LastIndexOf('t');
int index3 = charString.LastIndexOf('y');
Output:
index1 : 32
index2 : 31           
index3 : -1  

4) LastIndexOf(Char,Int32)

  1. LastIndexOf(Char,Int32) method returns the index position of last occurrence of supplied character.
  2. Search starts from specified startIndex position and proceeds backward toward first character position.
  3. Index numbering starts from zero.
  4. Returns -1 if character is not found.

Example:

string charString = "this is LastIndexOf string function.";
int index1 = charString.LastIndexOf('i',6);
int index2 = charString.LastIndexOf('t',30);
int index3 = charString.LastIndexOf('y');
Output:
index1 : 5
index2 : 21           
index3 : -1  

5) ToUpper()

ToUpper() method returns a new string in which all characters in the given string are converted to upper-case.

Example:

string charString = "this is ToUpper string function.";
       charString = charString.ToUpper();
Output:
THIS IS TOUPPER STRING FUNCTION.

6) ToLower()

ToLower() method returns a new string in which all characters in the given string are converted to lower-case.

Example:

string charString = "This is ToLower String FUNCTION.";
       charString = charString.ToLower();
Output:
this is tolower string function.

7) Trim()

Trim() method removes all leading and trailing white-space characters from given string.

Example:

string charString = "   This is Trim String FUNCTION.  ";
       charString = charString.Trim();
Output:
This is Trim String FUNCTION.

8) ToCharArray()

  1. ToCharArray() method copies each character of a given string to a character array.
  2. The first character copied is at index zero of the returned character array.
  3. The last character copied is at index Array.Length-1.

Example:

string InputString = "asparticles.com";
char[] CharArray = InputString.ToCharArray();
for (int i = 0; i < CharArray.Length; i++)
{
    Console.Write("Index: "+i+" ");
    Console.WriteLine("Character: "+CharArray[i]);
}
Console.ReadKey();
Output:
Index: 0 Character: a
Index: 1 Character: s
Index: 2 Character: p
Index: 3 Character: a
Index: 4 Character: r
Index: 5 Character: t
Index: 6 Character: i
Index: 7 Character: c
Index: 8 Character: l
Index: 9 Character: e
Index: 10 Character: s
Index: 11 Character: .
Index: 12 Character: c
Index: 13 Character: o
Index: 14 Character: m 

9) String concatenation

We can concatenate two or more than two strings using various methods. I have explained some of them below.

1) Using + operator

Example 1:
string string1 = "string1";
string string2 = "string2";
string string3 = string1 + string2;
Output:
string1string2
Example 2:
string string1 = "string1";
string string2 = "string2";
string string3 = string1 +" "+ string2; //adding blank space between two strings
Output:
string1 string2
Example 3:
string string1 = "string1";
string string2 = "string2";
string string3 = string1 + " " + string2+" "+"string3";
Output:
string1 string2 string3

2) Using += operator

Example 1:
string string1 = "string1";
string1 += "string2";
string1 += string1;
Output:
string1string2string1string2

3) Using string.Concat method

Example 1:
string string1 = "string1";
string string2 = "string2";
string string3= string.Concat(string1,string2);
Output:
string1string2
Example 2:
string string1 = "string1";
string string2 = "string2";
string string3 = string.Concat(string1," ",string2);
Output:
string1 string2
Example 3:
string string1 = "string1";
string string2 = "string2";
string string3 = string.Concat(string1," ",string2," ","string3");
Output:
string1 string2 string3

10) Compare(String,String,Boolean)

Compare(String,String,Boolean) method is used to compare two string based on the sort order of strings. The third parameter is boolean parameter, set true to ignore the case otherwise set false. Method returns integer value.

  1. Returns 0 if both the strings are equal.
  2. Returns 1 if string first string is greater than second.
  3. Returns - if string first string is less than second.

Example:

string StringA = "a";
string StringB = "b";
int result = string.Compare(StringA, StringB, true);
Console.WriteLine(result.ToString()); // returns -1   here a is less than b

StringA = "b";
StringB = "a";
result = string.Compare(StringA, StringB, true);
Console.WriteLine(result.ToString()); // returns 1  here b is greater than a

StringA = "ab";
StringB = "ba";
result = string.Compare(StringA, StringB, true);
Console.WriteLine(result.ToString()); // returns -1 

StringA = "bb";
StringB = "ba";
result = string.Compare(StringA, StringB, true);
Console.WriteLine(result.ToString()); // returns 1 

StringA = "bb";
StringB = "bc";
result = string.Compare(StringA, StringB, true);
Console.WriteLine(result.ToString()); // returns -1

StringA = "bba";
StringB = "bab";
result = string.Compare(StringA, StringB, true);
Console.WriteLine(result.ToString()); // returns 1

StringA = "bbb";
StringB = "bzb";
result = string.Compare(StringA, StringB, true);
Console.WriteLine(result.ToString()); // returns -1

StringA = "abcd";
StringB = "zzz";
result = string.Compare(StringA, StringB, true);
Console.WriteLine(result.ToString()); // returns -1

StringA = "abzd";
StringB = "abcz";
result = string.Compare(StringA, StringB, true);
Console.WriteLine(result.ToString()); // returns 1

11) Contains()

Contains() method returns a boolean value indicating whether a specified substring occurs within the given string. Returns true if supplied substring is occurs within string.

Example 1:

string stringSearch = "welcome to asparticles.com";
bool flag = stringSearch.Contains(".com");
Output:
true

Example 2:

string stringSearch = "welcome to asparticles.com";
bool flag = stringSearch.Contains(".COM");
Output:
false

12) Replace(Char,Char)

Replace(Char,Char) is used to replace all occurrences of specified character with new specified character in a given string.

Example 1:

string charString = "Welcome to asparticles,com";
charString = charString.Replace(',','.'); // replace ,(comma) with .(dot)
Output:
Welcome to asparticles.com

Example 2:

string charString = "1 2 3 4 5";
charString = charString.Replace(' ',','); // replace blank space with ,(comma)
Output:
 1,2,3,4,5 

13) Replace(String,String)

Replace(String,String) is used to replace all occurrences specified string with new specified string in a given string.

Example :

string charString = "this is replace functions";
charString = charString.Replace("functions", "function"); // replace "functions" with "function"
Output:
this is replace function

14) Substring(Int32)

Substring(Int32) is used to used retrieve part of string from specified index to end of the string.

Example :

string charString = "Welcome to asparticles.com";
charString = charString.Substring(11);
Output:
asparticles.com

15) Substring(Int32,Int32)

Substring(Int32,Int32) is used to used retrieve part of string from specified index up to specified length.

Example :

string charString = "asparticles.com";
charString = charString.Substring(0,11);
Output:
asparticles

..remaining I will explain you later

Share:

0 comments:

Post a Comment

Email Subscription

Subscribe to our newsletter to get the latest articles directly into your inbox

Blog Archive

BUY FROM AMAZON