[Mono-list] string expansion

Alan McGovern alan.mcgovern at gmail.com
Wed May 16 05:54:30 EDT 2007


Hi,

You can't extend the actual string class simply because those methods are
not part of the standard, hence you'd break compatibility with all other
implementations of .NET.

However you are free to use "Extension Methods" in your own code to add
those new methods as you require.

Alan.

On 5/16/07, Fabrício <skarllot at hotmail.com> wrote:
>
>  I look for anything like String.cs, in svn://anonsvn.mono-
> project.com/source/trunk/mono,  to submit my own methods do expands the
> String class, but I don't found...
>
> I have methods like these:
>
>   /// <summary>
>   /// Counts how many occurrences of the specified Unicode character in
> current string.
>   /// </summary>
>   /// <param name="value">A Unicode character to count.</param>
>   /// <returns>Result of number of found occurrences.</returns>
>   public int CountOf(char value)
>   {
>    if (this.Length == 0)
>     return 0;
>
>    int count = 0;
>    foreach (char c in this)
>     if (c == value)
>      count++;
>    return count;
>   }
>
>   /// <summary>
>   /// Counts how many occurrence of the specified string in current
> string.
>   /// </summary>
>   /// <param name="value">The string to count.</param>
>   /// <returns>Result of number of found occurrences.</returns>
>   /// <exception cref="ArgumentNullException"><c>value</c> is a null
> reference.</exception>
>   public int CountOf(string value)
>   {
>    if (value == null)
>     throw new ArgumentNullException(resExceptions.ArgumentNull.Replace("%var",
> "value"));
>
>    if (value.Length > this.Length || value.Length == 0)
>     return 0;
>
>    int count = 0;
>    int idx = 0;
>    int len = this.Length;
>
>    loop:
>     idx = this.IndexOf(value, idx);
>     if (idx != -1)
>     {
>      count++;
>      idx++;
>      goto loop;
>     }
>
>    return count;
>   }
>
>   /// <summary>
>   /// Indicates whether the current <see cref="String"/> contains only
> letters.
>   /// </summary>
>   /// <returns>true if current String is an letter-only String; otherwise,
> false.</returns>
>   /// <remarks>
>   /// This method verify whether a <see cref="String"/> contains only
> letters.
>   /// </remarks>
>   /// <example>
>   /// The following console application example show how to use
> IsLetterOnly method.
>   /// <code>
>   /// using System;
>   ///
>   /// class Program
>   /// {
>   ///  static void Main()
>   ///  {
>   ///   string text1 = "Make these tests";
>   ///   string text2 = "testing?";
>   ///   string text3 = "text3";
>   ///   string text4 = "example";
>   ///   Console.WriteLine("First example: {0}", text1.IsLetterOnly());
>   ///   Console.WriteLine("Second example: {0}", text2.IsLetterOnly());
>   ///   Console.WriteLine("Third example: {0}", text3.IsLetterOnly());
>   ///   Console.WriteLine("Fourth example: {0}", text4.IsLetterOnly());
>   ///   Console.ReadKey();
>   ///  }
>   /// }
>   /// </code>
>   /// <br></br>This example returns the following output.
>   /// <code>
>   /// First example: False
>   /// Second example: False
>   /// Third example: False
>   /// Fourth example: True
>   /// </code>
>   /// </example>
>   public bool IsLetterOnly()
>   {
>    for (int i = 0; i < this.Length; i++)
>    {
>     if (!char.IsLetter(this, i))
>      return false;
>    }
>    return true;
>   }
>
> I have more...
>
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20070516/0ab5095e/attachment.html 


More information about the Mono-list mailing list