public SqlString Substring(int startIndex, int length) { if (startIndex < 0) { throw new ArgumentException("startIndex should be greater than or equal to 0", "startIndex"); } if (length < 0) { throw new ArgumentException("length should be greater than or equal to 0", "length"); } SqlStringBuilder builder = BuildSubstring(startIndex); if (builder.Count == 0) { return(builder.ToSqlString()); } int offset = 0; int nextOffset = -1; int count = int.MaxValue; for (int i = 0; i < builder.Count; i++) { nextOffset = offset + LengthOfPart(builder[i]); if (nextOffset < length) { offset = nextOffset; continue; } else if (nextOffset >= length) { count = i + 1; break; } } while (builder.Count > count) { builder.RemoveAt(builder.Count - 1); } if (length < nextOffset) { string lastPart = (string)builder[builder.Count - 1]; builder[builder.Count - 1] = lastPart.Substring(0, length - offset); } SqlString result = builder.ToSqlString(); if (isCompacted) { result.SetCompacted(); } return(result); }
/// <summary> /// Retrieves a substring from this instance. The substring starts at a specified character position. /// </summary> /// <param name="startIndex">The starting character position of a substring in this instance.</param> /// <returns> /// A new SqlString to the substring that begins at startIndex in this instance. /// </returns> /// <remarks> /// If the startIndex is greater than the length of the SqlString then <see cref="SqlString.Empty" /> is returned. /// </remarks> public SqlString Substring(int startIndex) { if (startIndex < 0) { throw new ArgumentException("startIndex should be greater than or equal to 0", "startIndex"); } SqlStringBuilder builder = BuildSubstring(startIndex); if (builder.Count == 0) { return(Empty); } SqlString result = builder.ToSqlString(); if (isCompacted) { result.SetCompacted(); } return(result); }