/MAIN-OLD/Source/Facebook/Utility/StringHelper.cs
C# | 128 lines | 87 code | 10 blank | 31 comment | 12 complexity | 7b68eed7f44b79361b2d8db912cd18a7 MD5 | raw file
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Text;
- using facebook.Schema;
-
- namespace facebook.Utility
- {
- internal sealed class StringHelper
- {
- private StringHelper()
- {
- }
-
- /// <summary>
- /// Convert a collection of strings to a comma separated list.
- /// </summary>
- /// <param name="collection">The collection to convert to a comma separated list.</param>
- /// <returns>comma separated string.</returns>
- internal static string ConvertToCommaSeparated(IList<string> collection)
- {
- ///
- /// assumed that the average string length is 10 and double the buffer multiplying by 2
- /// if this does not fit in your case, please change the values
- ///
- int preAllocation = collection.Count * 10 * 2;
- var sb = new StringBuilder(preAllocation);
- int i = 0;
- foreach (string key in collection)
- {
- sb.Append(key);
- if (i < collection.Count - 1)
- sb.Append(",");
-
- i++;
- }
- return sb.ToString();
- }
- /// <summary>
- /// Convert a collection of strings to a comma separated list.
- /// </summary>
- /// <param name="collection">The collection to convert to a comma separated list.</param>
- /// <returns>comma separated string.</returns>
- internal static string ConvertToCommaSeparated(IList<int?> collection)
- {
- ///
- /// assumed that the average string length is 10 and double the buffer multiplying by 2
- /// if this does not fit in your case, please change the values
- ///
- int preAllocation = collection.Count * 10 * 2;
- var sb = new StringBuilder(preAllocation);
- int i = 0;
- foreach (int? key in collection)
- {
- sb.Append(key.ToString());
- if (i < collection.Count - 1)
- sb.Append(",");
-
- i++;
- }
- return sb.ToString();
- }
- /// <summary>
- /// Convert a collection of strings to a comma separated list.
- /// </summary>
- /// <param name="collection">The collection to convert to a comma separated list.</param>
- /// <returns>comma separated string.</returns>
- internal static string ConvertToCommaSeparated(IList<int> collection)
- {
- ///
- /// assumed that the average string length is 10 and double the buffer multiplying by 2
- /// if this does not fit in your case, please change the values
- ///
- int preAllocation = collection.Count * 10 * 2;
- var sb = new StringBuilder(preAllocation);
- int i = 0;
- foreach (int key in collection)
- {
- sb.Append(key.ToString());
- if (i < collection.Count - 1)
- sb.Append(",");
-
- i++;
- }
- return sb.ToString();
- }
-
- internal static string ConvertToCommaSeparated(IList<long> collection)
- {
- ///
- /// assumed that the average string length is 10 and double the buffer multiplying by 2
- /// if this does not fit in your case, please change the values
- ///
- int preAllocation = collection.Count * 10 * 2;
- var sb = new StringBuilder(preAllocation);
- int i = 0;
- foreach (long key in collection)
- {
- sb.Append(key.ToString());
- if (i < collection.Count - 1)
- sb.Append(",");
-
- i++;
- }
- return sb.ToString();
- }
- public static string StripNonValidXMLCharacters(string s)
- {
- StringBuilder _validXML = new StringBuilder(s.Length, s.Length); // Used to hold the output.
- char current; // Used to reference the current character.
- char[] charArray = s.ToCharArray();
-
- if (string.IsNullOrEmpty(s)) return string.Empty; // vacancy test.
-
- for (int i = 0; i < charArray.Length; i++)
- {
- current = charArray[i]; // NOTE: No IndexOutOfBoundsException caught here; it should not happen.
- if ((current == 0x9) ||
- (current == 0xA) ||
- (current == 0xD) ||
- ((current >= 0x20) && (current <= 0xD7FF)) ||
- ((current >= 0xE000) && (current <= 0xFFFD)))
- _validXML.Append(current);
- }
- return _validXML.ToString();
- }
-
- }
- }