aspose file tools*
The moose likes Java in General and the fly likes occurence of char in a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "occurence of char in a string" Watch "occurence of char in a string" New topic
Author

occurence of char in a string

suman deb
Ranch Hand

Joined: Jul 14, 2005
Posts: 54
hi all,

how to find out the occurence of each char in a string? e.g.

for the string "HELLO" , the required out put is--

H - 1
E - 1
L - 2
o - 1

please help
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

You could try with String.indexOf(int ch, int fromIndex), looping through the whole String.


[My Blog]
All roads lead to JavaRanch
Allan Morris
Greenhorn

Joined: Sep 03, 2006
Posts: 4
Hi, a map is a good data structure, pairing each character to a count. You can do it like this using the Collections framework:


[ September 05, 2006: Message edited by: Allan Morris ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: occurence of char in a string