In this tutorial you will learn how to do Java String counting elements
For the backend we will implement a method for each desired property and to go further we will create an HTML view to display the results with the help of Dukescript all in less than 30 minutes.
We can get the length of a text with the method length of the String class as follows:
int letters(String myText) {
return myText.length();
} To count the spaces of a string we will use a basic Regular Expression to identify the char whitespace. The whitespace character can be represented as [ ] (please note the space between the brackets).
int spaces(String myText) {
return myText.length()-myText.replaceAll("[ ]","").length();
} To count the vowels we are using RegEx
int vowels(String myText) {
return myText.length()-myText.replaceAll("[aeiouAEIOU]","").length();
} To count the consonants we will select all letters that are not present in the vowels regex.
int consonants(String myText) {
return myText.length()-myText.replaceAll("[b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z]","").length();
} As we can detect we need for the data model a variable of type String named myText, that is meant to hold the text to be analyzed.
class StringStatistics{
String myText;
public StringStatistics(myText){
this.myText=myText;
}
} Casting it to the dukescript way we are gonna have
@Model(className="StringStatistics", properties={
@Property(name="myText", type=String.class)
}) Next we are gonna create an HTML for the view like this
<textarea placeholder="Type a text to analyze...">
</textarea>
<p>
The string <pre></pre> has
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
</ul> Then we are gonna add the knockout bindings and our final HTML code becomes as following
<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> As we can detect we need for the data model a variable of type String named myText, that is meant to hold the text to be analyzed.
class StringStatistics{
String myText;
public StringStatistics(myText){
this.myText=myText;
}
} Casting it to the dukescript way we are gonna have
@Model(className="StringStatistics", properties={
@Property(name="myText", type=String.class)
}) Our final code will become as following
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 PrimeDemo {
@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();
}
}
Live demo
In case you have troubles visualizing the demo, you can find it on the following link: http://dew.apidesign.org/dew/#fd4a282ff87f23473914c68baae95d03
Te comparto aquí mis recomendaciones de cursos de Código Facilito. Codigofacilito.com es una plataforma virtual…
Te comparto aquí mis recomendaciones de cursos de Linkedin Learning. LinkedIn Learning es una plataforma…
Estos snippets de código para entrevista acerca de mockito espero te sean de mucha utilidad.…
En este tutorial aprenderá a usar expresiones regulares en Java de manera práctica para contar…
¿Entrevista de programación Java fullstack? No te preocupes, te tengo un compendio de preguntas de…
Te recomiendo algunos cursos de Udemy para mejorar tus softskills y así mejorar en tu…