Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Occurs in C#
Message
De
26/02/2013 13:25:28
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01566946
Message ID:
01566958
Vues:
45
This message has been marked as a message which has helped to the initial question of the thread.
>>>Hi everybody,
>>>
>>>Just wondering if occurs should be implemented the same way as in SQL Server?
>>>
>>>E.g.
>>>
>>>string.Length - String.replace(stringToTest,"").Length
>>>
>>
>>
>>What's wrong with String.Contains() ? Why do you need a replace()
>>
>>
>>
>>>
>>>Thanks in advance.
>>>
>>>Found this implementation also
>>>
>>>http://www.dotnetperls.com/string-occurrence
>>>
>>>wondering if I should go with my first idea or this one.
>>>
>>>Found this discussion
>>>
>>>http://stackoverflow.com/questions/541954/how-would-you-count-occurences-of-a-string-within-a-string-c
>>>
>>>I think I may need two separate methods - one for string and one for char.
>
>Contains returns boolean. I need number of occurrences. I checked that thread in stackoverflow, looks like it doesn't matter for a single test which method to implement. Just wondering if my current implementation is OK as is:
>
>
> /// <summary>
>      /// Occurs function
>      /// </summary>
>      /// <param name="source"></param>
>      /// <param name="pattern"></param>
>      /// <returns></returns>
>      public static Int32 Occurs(this String source, char pattern)
>      {
>         return source.Split(pattern).Length - 1;
>      }
>
>      public static Int32 Occurs(this String source, String pattern)
>      {
>         return (source.Length - source.Replace(pattern, "").Length)/pattern.Length;
>      }
From the link you provided this was supposed to be fasted for char matching:
int length = testchars.Length;
for (int n = length-1; n >= 0; n--)
{
    if (testchars[n] == '/')
        count++;
}
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform