I have a String of say, length is 5000. I want to find out the number of times the letter 'R' (Case Sensitive) is used. Below are the two passable solution...
Help me out with the efficient solution on the cards for this. Thanks.
try this one
String text = "ABCabcRRRRRrrr";
int count = text.length() - text.replace("R", "").length();
If you're using java >= 8 you can use the Streams:
public static void main(String args[]) {
String str= "abcderfgtretRetRotpabcderfgtretRetRotp"
System.out.println(str.chars().filter(c -> c == 'R').count());
}
String str = //the actual string
for(int i=0;i<str.length();++i)
{
if(str.charAt(i)=='R')
{
capitalRCount++;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.