You can use the StringBuilder class or simply create a new string by appending the indexes at [0], [1], [2], [3] (in the case that you always want the first 4 characters.
You can also create a Left function:
Console.Writeline(Left(myString, 4));
public static string Left(string param, int length)
{
string result = param.Substring(0, length);
return result;
}
Another thing you can do is create a string extension method:
static class StringExtensions
{
public static String Left(this string str, int numbOfChars)
{
if(str.Length <= numbOfChars) return str;
return str.Substring(0, numbOfChars);
}
public static String Right(this string str, int numbOfChars)
{
if numbOfChars >= str.Length) return str;
return str.Substring(str.Length, str.Length-numbOfChars);
}
}
You can call this like this:
String test = "Hello World";
String str = test.Left(3); //returns Hel