문제

I'm making some changes to a Java Tapestry application that I didn't build. I need to use a Java class to count the number of times a line break (/r) occurs in a string. Does Tapestry have a built-in tool for doing this, or a reference guide for string functions?

I can easily replace all instances of line breaks with

 textmedium.getTextcontent().replaceAll("\r", "<br>")

But I can't find a reference which would tell me what other functions I can use on textmedium.getTextcontent().

StackOverflow's favourite "count instances of x in a string using Java" answer (here: Java: How do I count the number of occurrences of a char in a String?) returns an error that there's no such thing as StringUtils, so I guess StringUtils is not a Tapestry thing, or I'm doing it wrong. Is there an equivalent I can use, or a fast way to import it?

도움이 되었습니까?

해결책

I'd suggest that you add Apache Commons Lang to your classpath. If you are not able to do so, you can find you own way to solve this issue, it's not too hard but using an existing, well-tested approach is probably better.

A simple solution could look as following:

public static void main(String[] args) {
    String bla =" gkjewlgjewl\rgjeklwgjewl\rwgjkewlgew";
    System.err.println(bla.split("\r").length - 1);
}

You could also use String's indexOf(int ch) method to find the first occurence and continue with indexOf(int ch, int fromIndex) until you don't find any more and count it up at the end.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top