String :
String is a class comes under System namespace. String class contain number of property and method to manipulate string. This methods and properties are used to initialize constructor, value assignment, and various operation on string. Here are some most common method of string class. One point should be keep in mind, that System.String not string are not same .String is a inbuilt type and variable is declared to hold a string array. System.String is a class under system namespace. Its instance is created to use it. Here is the example of string and Systems of string.
Here is the example of property and method of string.
Constructor :
Property :
String is a class comes under System namespace. String class contain number of property and method to manipulate string. This methods and properties are used to initialize constructor, value assignment, and various operation on string. Here are some most common method of string class. One point should be keep in mind, that System.String not string are not same .String is a inbuilt type and variable is declared to hold a string array. System.String is a class under system namespace. Its instance is created to use it. Here is the example of string and Systems of string.
Here is the example of property and method of string.
Constructor :
- string : string constructor create a instance of a string class to hold Unicode character in each position of a string array.
Property :
- char : string
class is array of Unicode character. Holds Unicode character in each
position.char keyword return character of a particular position.
string example1 = "I am Learing C Sharp";for (int i = 0; i <= example1.Length - 1; i++){Console.WriteLine(example1[i] + "
");}//output :I//output ://output :a//output :m//output ://output :L//output :e//output :a//output :r//output :i//output :n//output :g//output ://output :C//output ://output :S//output :h//output :a//output :r//output :p
- length : return the length of the array. The string class is
and array of Unicode character. The length property return the length
of the array or number of element in the array.
string example1 = "I am Learing C Sharp";Console.WriteLine(example1.Length);//Output:20
- Clone:Clone method create new array of same size and same character at same position. It create a duplicate of the existing array.
string example1 = "I am Learing C Sharp";string example2 = (String)example1.Clone();Console.WriteLine(example2);//Output:I am Learing C Sharp
- Compare : Compare between two string. If check two strings are exactly equal or not.if equal , return integer indication.
string example1 = "Hello World";string example2 = "I am Learing C Sharp";if (String.Compare(example1, example2) ==1){//Statement}
- Concat : Concat method is used to concatenation between two string .It return output of string that merge into single string. string example1 = "Hello World ";string example2 = "I am Learing C Sharp";string example3 = String.Concat(example1, example2);Console.WriteLine(example3);//Output :Hello World I am Learing C Sharp
- Copy :Copy method copy the string array to another string.
string example1 = "I am Learing C Sharp";string example2 = string.Copy(example1);Console.WriteLine(example2);//Output:I am Learing C Sharp
- EndsWith :This
method is used to check string ending with some specific character or
string. If string ending to match with the given character the method
return true or false .
string example1 = "I am Learing C Sharp";if (example1.EndsWith("Sharp") == true){}
- equal :It is also used for comparing string array. Check two strings are equal or not.
string example1 = "I am Learing C Sharp";string example2 = "Hellow world";if (example1.Equals(example2)){//statement}
- GetType : Get typed returns the type of instance.
string example1 = "I am Learing C Sharp";Console.WriteLine(example1.GetType());//System.String
- IsNullOrEmpty : IsNullOrEmpty is a method to check if string is empty string or null value is assigned in it.
string example3="";if (String.IsNullOrEmpty(example3) == true){//statement}
- Join:Join method concatenate all the element of array return a complete string.
string[] arr = { "I", "Am", "Learing" };string exaple1 = string.Join(",", arr);Console.WriteLine(string.Join(",", arr));//Output:I,Am,Learing
- PadLeft :This
method is used to make padding in the left side of the string array.
Character can be added left side of the string array. Number of
character to be added in the left will be depending on the method
parameter.
string example = "I am Learning";string example1 = example.PadLeft(20);Console.WriteLine(example1.Length);//Output:20
- PadRight : This
method is used to make padding in the right side of the string array.
Character can be added to the string array. Number of character to be
added in the right will be depending upon the method parameter.
string example = "I am Learning";string example1 = example.PadRight(20);Console.WriteLine(example1.Length);//Output:20
- Remove :Remove statement removes all the character from a string, starting point need to be mentioned.string example1 = "I am Learing C Sharp";string example2 = example1.Remove(5);Console.WriteLine(example2);//Output:I am
- Replace : Replace
is a method to replace some specific character from a string. It can be
single or multiple times. We can replace a sting with another string.
string example1 = "I am Learing C Sharp";string example2 = example1.Replace(" ", "X");Console.WriteLine(example2);//Output:IXamXLearingXCXSharp
- Split : Split Method, split a string with the help of deliminator
and return the array. Split method find position of each deliminator, separated the string in the deliminator position and return as an array.string example2 = "I am Learing C Sharp";string[] example3 = example2.Split(' ');
for (int i = 0; i < example3.Length; i++){Response.Write(example3[i] + "
");}//output :I//output :am//output :Learing//output :C//output :Sharp
- Substring :
Substring is a method to get part of string depending upon the method
parameter. Substring takes start and length argument and return part of a
string. StartIndex argument tails the method that the segment start of
string will be start from this index , length argument tails the method,
the part of string will be this length.
string example2 = "I am Learing C Sharp";string example3 = example2.Substring(5, 10);Console.WriteLine(example3);//Output:Learing C
- ToLower : Return the whole string to lower character. All character will be in small case.
string example2 = " I am Learing C Sharp ";string example3 = example2.ToLower();Console.WriteLine(example3);//Output:i am learing c sharp
- ToUpper : Return the whole string to Upper character. All character will be in capital case.
string example2 = " I am Learing C Sharp ";string example3 = example2.ToUpper();Console.WriteLine(example3);//Output:I AM LEARING C SHARP
- Trim : Trim
method remove the unnecessary space from a string array. If there is
any Space at the end of the string or at the beginning of the stream,
Trim method remove this unnecessary space. string example2 = " I am Learing C Sharp ";string example3 = example2.Trim();Console.WriteLine(example3);//Output:I am Learing C Sharp
No comments:
Post a Comment