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

void Update () {
	 cool=' ';
	foreach(char cool in abc)
	{asd=asd+1;}	
}

thanx

So, you want to count the number of occurrences of a specific character (space in your case) within a string? Here’s one way:

string source = "This is my source string";
int count = source.Split(' ').Length - 1;
1 Like

You’re the best, thanks:) hope you’ll help me in future :stuck_out_tongue:

Scusme, and if i would like to count how many words are there?

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:

string source = "This is my source string";
int wordCount = source.Split(' ').Length;

no, I mean:
string source = “This is my source string”;
int wordCount = source.Split(‘source’).Length;

Virtually the same thing.

// The important part is the string split, a more verbose way of representing it would be this:

string toSplit = "This is an example string.";

string[] split = toSplit.Split(' ');

foreach(string str in split){
	Debug.Log(str);
}

// This would output:

// "This"
// "is"
// "an"
// "example"
// "string."

// So the length of this "split" array is 5. To get the count of the spaces, he subtracted one
// To get the "word" count, just don't subtract that one

int wordCount = source.Split(' ').Length;

// This *may* not always be accurate though, depending on where your spaces are. A better way to handle it would be something like

int wordCount = 0;
foreach(string str in source.Split(' ')){
	if (string.isNullOrEmpty(str.Trim()) == false){
		wordCount++;
	}
}

// This way it only counts actual strings containing at least one non-space character 
// Otherwise something like " This is an example string. "; may return incorrect results

Oh, in that case you don’t want to split with a char, you want to split with a string.

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;

using System.LINQ;

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:

int count = (source.Length - source.Replace("source", "").Length) / "source".Length;

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?

Too many characters in character literal

Yea, for some reason I was thinking that would work - guess not. Tested this and it works great:

public static class StringExtensionMethods {
	public static int WordCount(this string s, string word){
		return (s.Length - s.Replace(word, "").Length) / word.Length;
	}
}

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

string source = "This is my source string";
Debug.Log(source.WordCount(" source"));

// Outputs "1"

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

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

And call it like

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

Ok, thanks man.

Hello I need some one help. I want to make a function to make a text read with sound