Part 22 String & String Builder
we will discuss1. String
2. String Builder
String
String is a reference type and behaves like value type. String is sequence of characters or array of characters.A string is represented by class System.String.
Example :
string name = "Chandan"; //creating string using string keyword
String lname = "Pandey"; //creating string using String class
string vs String
1.The string keyword is an alias for String.String and string are equivalentString lname = "Pandey"; //creating string using String class
3. String are immutable the contents of a string object.string value can't be changed after the object is created and It can contain nulls.
string name= "Chandan";
string Lname= name;
name+= "World";
System.Console.WriteLine(Lname);
//Output: chandan
4. String, string and System.String means same and it will not affect the performance of the application.
// "System.String" class
5.The String is use to the System.String class methods,
6. String object is created, the value is stored within the dynamic memory, or on the Heap.
7. string can contains null value and if you declare the value '0' then compile time error.
8. If any changes made to the string object like add or modify an existing value, then it will simply discard the old instance in memory and create a new instance to hold the new value.
String arrays:
String array create the array of string.
class Program
{
{
String[ ] s1 = new string[2];
s1 [0] = "chandan";
s1 [1] = "pandey";
for (int i = 0; i < 2; i++)
{
Console.WriteLine("value at Index position " + i + " is " + s1 [i]);
}
}
}
Output
value at Index position 0 is chandan
value at Index position 1 is pandey
class Program
{
{
string s1 = "chandan";
char[ ] ch = { 'c', 's', 'h', 'a', 'r', 'p' };
string s2 = new string(ch);
Console.WriteLine(s1);
Console.WriteLine(s2);
}
}
Output
chandan
csharp