CSharp examples for System:String SubString
Safely gets the substring depending on the length of original value and requested items Returns empty string if out of range.
using System;// ww w . ja v a 2s. c om public class Main{ /// <summary> /// Safely gets the substring depending on the length of original value and requested items /// Returns empty string if out of range. /// </summary> /// <param name="original"></param> /// <param name="startIndex"></param> /// <param name="length"></param> /// <returns></returns> public static string SafeSubstring(this string original, int startIndex, int length) { if (original.Length >= (startIndex + length)) { return original.Substring(startIndex, length); } else { if (original.Length > startIndex) { return original.Substring(startIndex); } else { return string.Empty; } } } }