Count char in string

Discussion in 'Scripting' started by sheva, Jun 30, 2011.

  1. sheva

    sheva

    Member

    Joined:
    Nov 22, 2010
    Messages:
    157
    Hi guys, how can i count, how many times the SpaceBar is pressed while writing the string:
    With this code, it sais that:
    `cool' conflicts with a declaration in a child block


    Code (csharp):
    1. void Update () {
    2.      cool=' ';
    3.     foreach(char cool in abc)
    4.     {asd=asd+1;}   
    5. }
    6.  
    thanx
     
  2. jgodfrey

    jgodfrey

    Member

    Joined:
    Nov 14, 2009
    Messages:
    539
    So, you want to count the number of occurrences of a specific character (space in your case) within a string? Here's one way:

    Code (csharp):
    1.  
    2. string source = "This is my source string";
    3. int count = source.Split(' ').Length - 1;
    4.  
     
  3. sheva

    sheva

    Member

    Joined:
    Nov 22, 2010
    Messages:
    157
    You're the best, thanks:) hope you'll help me in future :p
     
  4. sheva

    sheva

    Member

    Joined:
    Nov 22, 2010
    Messages:
    157
    Scusme, and if i would like to count how many words are there?
     
  5. jgodfrey

    jgodfrey

    Member

    Joined:
    Nov 14, 2009
    Messages:
    539
    If you assume words are delimited by the "space" character, it'd be almost exactly what I already posted, except that you wouldn't want to subtract 1 from the result. So:

    Code (csharp):
    1.  
    2. string source = "This is my source string";
    3. int wordCount = source.Split(' ').Length;
    4.  
     
  6. sheva

    sheva

    Member

    Joined:
    Nov 22, 2010
    Messages:
    157
    no, I mean:
    string source = "This is my source string";
    int wordCount = source.Split('source').Length;
     
  7. KyleStaves

    KyleStaves

    Member

    Joined:
    Nov 4, 2009
    Messages:
    806
    Virtually the same thing.

    Code (csharp):
    1.  
    2. // The important part is the string split, a more verbose way of representing it would be this:
    3.  
    4. string toSplit = "This is an example string.";
    5.  
    6. string[] split = toSplit.Split(' ');
    7.  
    8. foreach(string str in split){
    9.     Debug.Log(str);
    10. }
    11.  
    12. // This would output:
    13.  
    14. // "This"
    15. // "is"
    16. // "an"
    17. // "example"
    18. // "string."
    19.  
    20. // So the length of this "split" array is 5. To get the count of the spaces, he subtracted one
    21. // To get the "word" count, just don't subtract that one
    22.  
    23. int wordCount = source.Split(' ').Length;
    24.  
    25. // This *may* not always be accurate though, depending on where your spaces are. A better way to handle it would be something like
    26.  
    27. int wordCount = 0;
    28. foreach(string str in source.Split(' ')){
    29.     if (string.isNullOrEmpty(str.Trim()) == false){
    30.         wordCount++;
    31.     }
    32. }
    33.  
    34. // This way it only counts actual strings containing at least one non-space character
    35. // Otherwise something like " This is an example string. "; may return incorrect results
    36.  
     
  8. KyleStaves

    KyleStaves

    Member

    Joined:
    Nov 4, 2009
    Messages:
    806
    Oh, in that case you don't want to split with a char, you want to split with a string.

    Code (csharp):
    1.  
    2. int wordCount = source.Split("source").Length - 1;
    3.  
    If you want, you can use LINQ and get a nice reliable function that doesn't rely on a foreach loop to ensure accurate results in all conditions;

    Code (csharp):
    1.  
    2. using System.LINQ;
    3.  
    4. int wordCount = souce.Count(s => s == "source");
    5.  
    EDIT:

    Actually, if you don't want to use LINQ you can get accurate results from something like:

    Code (csharp):
    1.  
    2. int count = (source.Length - source.Replace("source", "").Length) / "source".Length;
    3.  
     
    Last edited: Jun 30, 2011
  9. sheva

    sheva

    Member

    Joined:
    Nov 22, 2010
    Messages:
    157
    Ok, but if I would to count how many word like" source" are there in the string... I can't use myString.split('source'); ... so what have I to use in this case?
     
  10. sheva

    sheva

    Member

    Joined:
    Nov 22, 2010
    Messages:
    157
    Too many characters in character literal
     
  11. KyleStaves

    KyleStaves

    Member

    Joined:
    Nov 4, 2009
    Messages:
    806
    Yea, for some reason I was thinking that would work - guess not. Tested this and it works great:

    Code (csharp):
    1.  
    2. public static class StringExtensionMethods {
    3.     public static int WordCount(this string s, string word){
    4.         return (s.Length - s.Replace(word, "").Length) / word.Length;
    5.     }
    6. }
    7.  
    Put that in it's own file, or on top of some other file - basically anywhere you want. Then you can do this...


    Code (csharp):
    1.  
    2. string source = "This is my source string";
    3. Debug.Log(source.WordCount(" source"));
    4.  
    5. // Outputs "1"
    6.  
    If you don't want to use extension methods, just use this function:

    Code (csharp):
    1.  
    2. public static int WordCount(string s, string word){
    3.         return (s.Length - s.Replace(word, "").Length) / word.Length;
    4.     }
    5.  
    And call it like

    Code (csharp):
    1.  
    2. int wordCount = YourClass.WordCount(source, " source");
    3.  
     
  12. sheva

    sheva

    Member

    Joined:
    Nov 22, 2010
    Messages:
    157
    Ok, thanks man.