C# string

string initialization and properties:

string s = "c sharp";
s += " tutorial";
s.Length; //7

string methods:
string[] arr = s.Split(' ');
int i = s.IndexOf('h');
s.Replace('h', 't');
s.ToLower();
s.ToUpper();
s.Trim();
s.TrimEnd();
s.TrimStart();
bool b = s.Contains("ar");
string s2 = s.PadLeft(4, "%"); //s2 = "%%%%c sharp";
string s2 = s.PadRight(4, "%"); //s2 = "c sharp%%%%"
string s2 = s.Substring(2, 3); //s2 = "sha"
string s2 = s.Remove(2, 3); //s2 = "c rp"
string s2 = s.Insert(2,"KK"); //s2 = "c KKsharp"

string can be used as a char array:
string s2 = "";
for (int i = s.Length; i > 0; i--)
{
s2 += s[i];
}
//s2 = "prahs c";

Convert string to numbers:
using System;
int i = 3;
double d = Convert.ToDouble(i);
uint ui = Convert.ToUInt32(i);
string s = Convert.ToString(i);
string s2 = "23.34";
double d2 = Convert.ToDouble(s);



endmemo.com © 2017  Terms of Use | Home