Unity Community


Results 1 to 12 of 12

  1. Location
    Vicenza, Italy
    Posts
    157

    Count char in string

    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:  
    1. void Update () {
    2.      cool=' ';
    3.     foreach(char cool in abc)
    4.     {asd=asd+1;}   
    5. }
    thanx


  2. Location
    Missouri, USA
    Posts
    528
    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:  
    1. string source = "This is my source string";
    2. int count = source.Split(' ').Length - 1;


  3. Location
    Vicenza, Italy
    Posts
    157
    You're the best, thanks hope you'll help me in future :P


  4. Location
    Vicenza, Italy
    Posts
    157
    Scusme, and if i would like to count how many words are there?


  5. Location
    Missouri, USA
    Posts
    528
    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:  
    1. string source = "This is my source string";
    2. int wordCount = source.Split(' ').Length;


  6. Location
    Vicenza, Italy
    Posts
    157
    no, I mean:
    string source = "This is my source string";
    int wordCount = source.Split('source').Length;


  7. Location
    New Jersey
    Posts
    791
    Virtually the same thing.

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


  8. Location
    New Jersey
    Posts
    791
    Quote Originally Posted by Sheva View Post
    no, I mean:
    string source = "This is my source string";
    int wordCount = source.Split('source').Length;
    Oh, in that case you don't want to split with a char, you want to split with a string.

    Code:  
    1. int wordCount = source.Split("source").Length - 1;

    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:  
    1. using System.LINQ;
    2.  
    3. int wordCount = souce.Count(s => s == "source");

    EDIT:

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

    Code:  
    1. int count = (source.Length - source.Replace("source", "").Length) / "source".Length;
    Last edited by KyleStaves; 06-30-2011 at 07:02 AM.


  9. Location
    Vicenza, Italy
    Posts
    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. Location
    Vicenza, Italy
    Posts
    157
    Quote Originally Posted by KyleStaves View Post
    Oh, in that case you don't want to split with a char, you want to split with a string.

    Code:  
    1. int wordCount = source.Split("source").Length - 1;
    Too many characters in character literal


  11. Location
    New Jersey
    Posts
    791
    Yea, for some reason I was thinking that would work - guess not. Tested this and it works great:

    Code:  
    1. public static class StringExtensionMethods {
    2.     public static int WordCount(this string s, string word){
    3.         return (s.Length - s.Replace(word, "").Length) / word.Length;
    4.     }
    5. }

    Put that in it's own file, or on top of some other file - basically anywhere you want. Then you can do this...


    Code:  
    1. string source = "This is my source string";
    2. Debug.Log(source.WordCount(" source"));
    3.  
    4. // Outputs "1"

    If you don't want to use extension methods, just use this function:

    Code:  
    1. public static int WordCount(string s, string word){
    2.         return (s.Length - s.Replace(word, "").Length) / word.Length;
    3.     }

    And call it like

    Code:  
    1. int wordCount = YourClass.WordCount(source, " source");


  12. Location
    Vicenza, Italy
    Posts
    157
    Ok, thanks man.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •