Re: StringBuilder Extension: IsQuotedBy
Hugh Brown <[email protected]> Thu, 14 Feb 2008 07:06:51 -0800
| Newsgroups | gmane.comp.windows.devel.dotnet.clr |
|---|---|
| Message-ID | <[email protected]> |
Is your code the same as this:
public static bool IsQuotedBy(string sb, string quoteString)
{
return sb.StartsWith(quoteString) && sb.EndsWith(Reverse(quoteString));
}
public static bool Reverse(string s)
{
StringBuilder sb = new StringBuilder();
for (int i = s.Length - 1; i >= 0; i--)
sb.Append(s[i]);
return sb.ToString();
}
Brady Kelly <[email protected]> wrote: I was playing around with avoiding, at all costs, creating surplus strings,
and came up with the following method to see if a StringBuilder, that I use
for each of a collection of string fields, is surrounded by a certain
string, e.g. double quotes. I was just wondering if this is an efficiant
way of doing it:
public static bool IsQuotedBy(this StringBuilder sb, string
quoteString)
{
// Check if the first characters match the quote string.
for (int i = 0; i < quoteString.Length; i++)
{
if (sb[i] != quoteString[i])
{
return false;
}
}
// Check if the last characters match the quote string.
for (int i = sb.Length - quoteString.Length; i < sb.Length; i++
)
{
int qsIndex = 0;
if (sb[i] != quoteString[qsIndex++])
{
return false;
}
}
return true;
}
===================================
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
===================================
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com