Can anyone help me to write code that find how many characters string has.
Asked
Active
Viewed 45 times
-3
-
Solution here: [link](http://stackoverflow.com/questions/767759/occurrences-of-substring-in-a-string) Please search before you ask your next question. Regards – Klatschen Apr 17 '16 at 15:14
-
@Nyunus Why cant you write a simple for loop for this? – Raman Shrivastava Apr 17 '16 at 15:28
-
@RamanShrivastava How pls help me – Nyunus Apr 17 '16 at 16:01
-
@Nyunus Posted one answer. Plz check. – Raman Shrivastava Apr 17 '16 at 16:05
-
@Nyunus So your question is entirely changed now. You just need string length? – Raman Shrivastava Apr 17 '16 at 16:16
-
@RamanShrivastava yes just length can you pls edit code below like – Nyunus Apr 17 '16 at 16:24
-
@Nyunus Already did. Check again. – Raman Shrivastava Apr 17 '16 at 16:25
-
@RamanShrivastava thank u very much. By the way do you have facebook account I have just very small things to ask – Nyunus Apr 17 '16 at 16:25
-
posted in my answer. – Raman Shrivastava Apr 17 '16 at 16:28
-
@RamanShrivastava I wrote to you can u pls check on there – Nyunus Apr 17 '16 at 16:30
2 Answers
1
All you need is - int len = yourString.length();

Raman Shrivastava
- 2,923
- 15
- 26
-
this is ok but this is not exactly what I look for. Can you pls edit this with different way if you know? – Nyunus Apr 17 '16 at 16:16
-
@Nyunus Can you provide one example of input and output. I will edit it accordingly. – Raman Shrivastava Apr 17 '16 at 16:17
-
-
0
StringUtils should provide you with something that you are looking for:
int count = StringUtils.countMatches("haahaahaa", "a");
System.out.println(count);

The Roy
- 2,178
- 1
- 17
- 33
-
So just for a small task like this you will add 3rd party jar? – Raman Shrivastava Apr 17 '16 at 15:26
-
The person who asked the question did not ask for an algorithm here and I don't see a reason why you need to reinvent the wheel everytime. Assume that for such common functions apache thought what sort of standard utils to provide. – The Roy Apr 17 '16 at 15:33
-