0

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?

Community
  • 1
  • 1
Ila
  • 3,528
  • 8
  • 48
  • 76
  • StringUtils is part of Commons Lang. – Thilo Aug 21 '13 at 12:54
  • [StringUtils](http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html) is a library from [Apache Commons Lang](http://commons.apache.org/proper/commons-lang/). You have to add it to your classpath if you want to use it. Which build tool do you have in place? – u6f6o Aug 21 '13 at 12:55

1 Answers1

2

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.

u6f6o
  • 2,050
  • 3
  • 29
  • 54
  • Thanks for the simple solution, it does exactly what I need. – Ila Aug 21 '13 at 13:30
  • @Ali although the solution works, I'd recommend to add a library as Apache Commons Lang to your class path that focuses exactly on such problems. Besides the solution for your problem, you also get a lot of useful helper methods for string processing etc. – u6f6o Aug 21 '13 at 14:05