Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. We're running great holiday deals on subscriptions, swag and Asset Store packages! Take a peek at this blog for more information!
    Dismiss Notice
  3. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  4. Unity 2017.2 is now released.
    Dismiss Notice
  5. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  6. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  8. Unity 2017.3 beta is now available for download.
    Dismiss Notice

Count char in string

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

  1. sheva

    sheva

    Joined:
    Nov 22, 2010
    Posts:
    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

    Joined:
    Nov 14, 2009
    Posts:
    565
    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

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

    sheva

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

    jgodfrey

    Joined:
    Nov 14, 2009
    Posts:
    565
    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

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

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    820
    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

    Joined:
    Nov 4, 2009
    Posts:
    820
    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

    Joined:
    Nov 22, 2010
    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. sheva

    sheva

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

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    820
    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

    Joined:
    Nov 22, 2010
    Posts:
    157
    Ok, thanks man.
     
  13. Shapon-Islam

    Shapon-Islam

    Joined:
    Jul 8, 2015
    Posts:
    1
    Hello I need some one help. I want to make a function to make a text read with sound