Skip to content

Instantly share code, notes, and snippets.

@javatlacati
Forked from jtulach/Prime.java
Last active August 11, 2016 23:04
Show Gist options
  • Save javatlacati/fd4a282ff87f23473914c68baae95d03 to your computer and use it in GitHub Desktop.
Save javatlacati/fd4a282ff87f23473914c68baae95d03 to your computer and use it in GitHub Desktop.
Simple text statistics
<textarea
data-bind="value: myText, valueUpdate: 'afterkeydown'"
placeholder="Type a text to analyze...">
</textarea>
<p>
The string <pre data-bind="text: myText"></pre> has
<ul>
<li data-bind="text: letters()+' letters.'"></li>
<li data-bind="text: vowels()+' vowels.'"></li>
<li data-bind="text: consonants()+' consonants.'"></li>
<li data-bind="text: spaces()+' spaces.'"></li>
</ul>
package dew.demo.string;
import net.java.html.json.Model;
import net.java.html.json.ComputedProperty;
import net.java.html.json.Property;
@Model(className="StringStatistics", properties={
@Property(name="myText", type=String.class)
})
class StringDemo {
@ComputedProperty static int letters(String myText) {
return myText.length();
}
@ComputedProperty static int vowels(String myText) {
return myText.length()-myText.replaceAll("[aeiouAEIOU]","").length();
}
@ComputedProperty static int consonants(String myText) {
return myText.length()-myText.replaceAll("[b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z]","").length();
}
@ComputedProperty static int spaces(String myText) {
return myText.length()-myText.replaceAll("[ ]","").length();
}
static {
new StringStatistics("").applyBindings();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment