Substring not working as expected if length greater than length of String
Quick and dirty:
A.Length > 40 ? A.Substring(0, 40) : A
Why not create an extension for it... call it Truncate or Left, or whatever.
public static class MyExtensions
{
public static string Truncate(this string s, int length)
{
if(s.Length > length) return s.Substring(0, length);
return s;
}
}
Then you can simply call it like so:
string B = A.Truncate(40);
Also note that you don't have to make it an extension method, although it would be cleaner.
In your StringTool class:
public static string Truncate(string value, int length)
{
if(value.Length > length) return value.Substring(0, length);
return value;
}
And to call it:
string B = StringTool.Truncate(A, 40);
String.Concat does not serve your purpose here. You should rather do the following:
if(A.Length > 40)
B= A.Substring(0,40);
else
B=A;