Getting x characters from the right side of a C# with Example



Getting x characters from the right side of a C# with Example

string 
Visual Basic has Left, Right, and Mid functions that returns characters from the Left, Right, and Middle of a string. 
These methods does not exist in C#, but can be implemented with Substring(). They can be implemented as an 
extension methods like the following: 
public static class StringExtensions 
{ 
///  
/// VB Left function 
///  
///   
///  
/// Left-most numchars characters 
public static string Left( this string stringparam, int numchars ) 
{ 
// Handle possible Null or numeric stringparam being passed 
stringparam += string.Empty; 
// Handle possible negative numchars being passed 
numchars = Math.Abs( numchars ); 
// Validate numchars parameter 
if (numchars > stringparam.Length) 
numchars = stringparam.Length; 
return stringparam.Substring( 0, numchars ); 
} 
///  
/// VB Right function 
///  
///   
///  
/// Right-most numchars characters 
public static string Right( this string stringparam, int numchars ) 
{ 
// Handle possible Null or numeric stringparam being passed 
stringparam += string.Empty; 
// Handle possible negative numchars being passed 
numchars = Math.Abs( numchars ); 
// Validate numchars parameter 
if (numchars > stringparam.Length) 
numchars = stringparam.Length; 
return stringparam.Substring( stringparam.Length - numchars ); 
} 
///  
/// VB Mid function - to end of string 
///  
///   
/// VB-Style startindex, 1st char startindex = 1 
/// Balance of string beginning at startindex character 
public static string Mid( this string stringparam, int startindex ) 
 

{ 
// Handle possible Null or numeric stringparam being passed 
stringparam += string.Empty; 
// Handle possible negative startindex being passed 
startindex = Math.Abs( startindex ); 
// Validate numchars parameter 
if (startindex > stringparam.Length) 
startindex = stringparam.Length; 
// C# strings are zero-based, convert passed startindex 
return stringparam.Substring( startindex - 1 ); 
} 
///  
/// VB Mid function - for number of characters 
///  
///   
/// VB-Style startindex, 1st char startindex = 1 
/// number of characters to return 
/// Balance of string beginning at startindex character 
public static string Mid( this string stringparam, int startindex, int numchars) 
{ 
// Handle possible Null or numeric stringparam being passed 
stringparam += string.Empty; 
// Handle possible negative startindex being passed 
startindex = Math.Abs( startindex ); 
// Handle possible negative numchars being passed 
numchars = Math.Abs( numchars ); 
// Validate numchars parameter 
if (startindex > stringparam.Length) 
startindex = stringparam.Length; 
// C# strings are zero-based, convert passed startindex 
return stringparam.Substring( startindex - 1, numchars ); 
} 
} 
This extension method can be used as follows: 
string myLongString = "Hello World!"; 
string myShortString = myLongString.Right(6); // "World!" 
string myLeftString = myLongString.Left(5); // "Hello" 
string myMidString1 = myLongString.Left(4); // "lo World" 
string myMidString2 = myLongString.Left(2,3);  // "ell" 

0 Comment's

Comment Form