Posted By

gndprx on 04/17/07


Tagged

c right Left MID


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

umang_nine
Priestd09
danfinrud55


Left, Right and Mid functions in C#


 / Published in: C#
 

  1. namespace LeftRightMid
  2. {
  3. ///
  4. /// Summary description for Class1.
  5. ///
  6. class LeftRightMid
  7. {
  8. ///
  9. /// The main entry point for the application.
  10. ///
  11. [STAThread]
  12. static void Main(string[] args)
  13. {
  14.  
  15. //assign a value to our string
  16. string myString = "This is a string";
  17. //get 4 characters starting from the left
  18. Console.WriteLine(Left(myString,4));
  19. //get 6 characters starting from the right
  20. Console.WriteLine(Right(myString,6));
  21. //get 4 characters starting at index 5 of the string
  22. Console.WriteLine(Mid(myString,5,4));
  23. //get the characters from index 5 up to the end of the string
  24. Console.WriteLine(Mid(myString,5));
  25. //display the result to the screen
  26. Console.ReadLine();
  27. }
  28.  
  29. public static string Left(string param, int length)
  30. {
  31. //we start at 0 since we want to get the characters starting from the
  32. //left and with the specified lenght and assign it to a variable
  33. string result = param.Substring(0, length);
  34. //return the result of the operation
  35. return result;
  36. }
  37. public static string Right(string param, int length)
  38. {
  39. //start at the index based on the lenght of the sting minus
  40. //the specified lenght and assign it a variable
  41. string result = param.Substring(param.Length - length, length);
  42. //return the result of the operation
  43. return result;
  44. }
  45.  
  46. public static string Mid(string param,int startIndex, int length)
  47. {
  48. //start at the specified index in the string ang get N number of
  49. //characters depending on the lenght and assign it to a variable
  50. string result = param.Substring(startIndex, length);
  51. //return the result of the operation
  52. return result;
  53. }
  54.  
  55. public static string Mid(string param,int startIndex)
  56. {
  57. //start at the specified index and return all characters after it
  58. //and assign it to a variable
  59. string result = param.Substring(startIndex);
  60. //return the result of the operation
  61. return result;
  62. }
  63.  
  64. }
  65. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: jrusse42 on March 4, 2009

This code worked great until I needed to pass strings that have unknown lengths. I added a little logic to the Left() function to allow for unknown string sizes:

[CODE] // Return left part of string public static string Left(string param, int length) { // Make sure the string is at least that long to begin with if (length < param.Length) return param.Substring(0, length); else return param; } [/CODE]

Posted By: sickpuppysoftware on July 27, 2010

Added length checks private static string Right(string param, int length) { string result = "";

//do a length check first so we don't  cause an error
//if length is longer than the string length then just get
//the whole string.
if (param.Length > 0)
{
    if (length > param.Length)
    {
        length = param.Length;
    }

    //start at the index based on the length of the string minus
    //the specified length and assign it a variable
    result = param.Substring(param.Length - length, length);
}
//return the result of the operation
return result;

}

private static string Left(string param, int length) { string result = "";

//do a length check first so we don't  cause an error
//if length is longer than the string length then just get
//the whole string.
if (param.Length > 0)
{
    if (length > param.Length)
    {
        length = param.Length;
    }

    //start at the beginning and get
    //the specified length and assign it a variable
    result = param.Substring(0, length);
}
//return the result of the operation
return result;

}

private static string Mid(string param, int start, int length) { string result = "";

//do a length check first so we don't  cause an error
//if length is longer than the string length then just get
//the rest of the string.
if ((param.Length > 0) && (start < param.Length))
{
    if (start + length > param.Length)
    {
        length = param.Length - start;
    }

    result = param.Substring(start, length);
}
//return the result of the operation
return result;

}

You need to login to post a comment.