Simplifying Java Software Development: How to Count the Number of Words in a String using Java (one-liner) Semplificare lo sviluppo del software Java: Come contare il numero di parole in una stringa usando Java (un-linea)
Java is a language of choice for millions of developers worldwide. Java è un linguaggio di scelta per milioni di sviluppatori in tutto il mondo. In a series of articles I will show simple tips and techniques which make Java extremely powerful and yet simple to use. In una serie di articoli mi mostrerà semplici suggerimenti e tecniche che rendono Java estremamente potente, eppure semplice da usare. Today’s article is about using regex, a pattern matcher incorporated in Java (from 1.4 I believe). L'articolo di oggi è di circa usando regex, un pattern matcher incorporati in Java (da 1,4 credo).
Here is a sample code (line in bold) to count the number of words in any amount of text. Qui c'è un esempio di codice (linea in grassetto) per contare il numero di parole in qualsiasi quantità di testo. The sample program counts the number of words in the argument to the program. Il programma di esempio conta il numero di parole in argomento per il programma. The argument must be quoted to ensure separate words are clubbed together in a single sentence by the operating system. L'argomento deve essere indicato al fine di garantire separare le parole sono clubbed insieme in una sola frase dal sistema operativo.
public class WordCount { public static void main(String args[]) { System.out.println(java.util.regex.Pattern.compile(”[\\w]+”).split(args[0].trim()).length); } } classe pubblica Wordcount (public static void main (String args []) (System.out.println (java.util.regex.Pattern.compile ( "[\ \ w ]+"). split (args [0]. trim ( )). lunghezza);)) Sample usage: Esempi di utilizzo:
java WordCount “This is a sample phrase with 8 words” java Wordcount "Questo è un esempio di frase con 8 parole"
All its does is compile the regular expression “\w” which matches with words and splits the phrase in an array of individual words. Tutti i suoi non è compilare l'espressione regolare "\ w", che corrisponde con le parole e con la frase divide in una serie di singole parole. Then the total length of the array is printed. Quindi la lunghezza totale delle serie è stampato.
Filed under Elencato sotto Headline News Headline News , How To Come , Java Software Software Java | |
| |
RSS 2.0 RSS 2,0 | |
Email this Article Invia questo articolo
You may also like to read Si può anche leggere come |





June 19th, 2005 at 12:02 am 19 giugno 2005 alle 12:02 am
Come on! Come on! There was absolutely no content in the feed. Non vi è stato assolutamente nessun contenuto del feed. Not even a teaser. Nemmeno un occhiolino. Just ads. Solo annunci.
Can you point me to one of your feeds with some content (and preferably no ads), or would you rather me unsubscribe? Potete punto per me uno dei tuoi feed con un certo contenuto (e, preferibilmente, non annunci), o se lo desiderate, potete cancellare l'iscrizione di me?
June 19th, 2005 at 12:16 am 19 giugno 2005 alle 12:16 am
That’s is very surprising. Che è molto sorprendente. Thanks for pointing it out. Grazie per puntamento fuori. It was working well before. E 'stato molto prima di lavoro. I tested it several times. Ho provato più volte.
I am working to debug the problem. Sto lavorando per il debug del problema. Can you tell me approximately from when the feed started spewing only ads? Può dirmi circa da quando ha iniziato il feed spewing solo gli annunci?
BTW: I stopped the Ad engine for now, till I rectify the problem. BTW: mi sono fermato il motore di annunci per ora, fino al I rettificare il problema. However it seems the problem is elsewhere. Tuttavia sembra che il problema è altrove. The actual feed content is not getting posted in feed. L'effettivo contenuto dei feed non viene pubblicato nei mangimi.
June 19th, 2005 at 12:22 am 19 giugno 2005 a 12:22 am
Here is what I got: Ecco ciò che ho ricevuto:
Sponsor(s): Sponsor (s):
Tired of dealing with messy toner, lost fax pages and unreliable fax machines? Stanchi di trattare con disordinati toner, fax perso pagine e inaffidabili fax? Why pour more money into extra phone lines and machine maintenance when you can send and receive faxes by email? Perché per più soldi extra in linee telefoniche e di manutenzione della macchina quando si possono inviare e ricevere fax tramite email? eFax is the #1 paperless faxing choice of individuals and businesses worldwide. eFax è il # 1 di supporti cartacei fax scelta degli individui e delle imprese in tutto il mondo. Get a local fax number to send and receive faxes right from your email inbox. Ottenere un numero di fax locale di inviare e ricevere fax direttamente dalla tua casella di posta in arrivo. It’s the smart way to access and organize your faxed documents. E 'l'astuzia di accesso e di organizzare i tuoi documenti via fax. Try eFax Plus free for a month. EFax Plus provare gratuitamente per un mese.
You are on the phone with your client, going through an RFP that you emailed them. Sei al telefono con il tuo client, passando attraverso un bando che avete inviato. You hear them flip through the pages. Li si sente scorrere le pagine. Suddenly, they ask: “How much will this cost?” This happens every day, right? Improvvisamente, si chiedono: "Qual è il costo?" Questo succede ogni giorno, giusto? Not with GoToMeeting, the easiest way to hold online meetings. Non con GoToMeeting, il modo più semplice per tenere riunioni on-line. You can present, share, and field questions - right from your desk. È possibile presentare, condividere, domande e campo - direttamente dalla tua scrivania. So you stop the “skip ahead syndrome” before it starts. In modo da fermare la "sindrome di passare direttamente" prima del suo inizio. Try it FREE now. Provalo gratis ora.
© 2005 blog.taragana.com. © 2005 blog.taragana.com. This RSS Feed is for personal non-commercial use only. Questo feed RSS è personale per uso non commerciale. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Se non siete lettura di questo materiale nel vostro aggregatore di notizie, il sito che stai vedendo è colpevole di violazione del diritto d'autore. Please contact Si prega di contattare legal@taragana.com so we can take legal action immediately. in modo da poter agire in giudizio immediatamente.
June 19th, 2005 at 12:29 am 19 giugno 2005 alle 12:29 am
Can you please tell me from when it started happening? Potresti dirmi da quando ha cominciato succedendo?
I saw the problem with the main feed and some other feeds like technology and web feeds. Ho visto il problema con i principali mangimi e di alcuni altri feeds come tecnologia e web feed.
However diabetes feed seems to be ok. Tuttavia il diabete mangimi sembra essere ok.
Looks like a problem with some characters in some posts. Si presenta come un problema con alcuni caratteri in alcuni posti. I am trying to find it. Sto cercando di trovarlo. Debugging PHP is such a royal pain! Mettere a punto PHP è un dolore reale!
June 19th, 2005 at 12:33 am 19 giugno 2005 alle 12:33 am
I think it started with this one: Penso che è iniziato con questa:
http://blog.taragana.com/index.php/archive/what-is-your-favorite-programming-language/
June 19th, 2005 at 12:52 am 19 giugno 2005 alle 12:52 am
Thanks. Grazie.
Can you please try it again and let me know if the problem is fixed on your end. Potete vi preghiamo di provare di nuovo e mi permetta di sapere se il problema è stato risolto sul tuo fine. You will have to refresh the feed. Sarà necessario aggiornare il feed. You can use any feed url. È possibile utilizzare qualsiasi URL del feed.
I have tested here on two offline feed aggregators. Ho testato qui non in linea su due aggregatori di feed.
The problem was with Y!Q (Related Search) plugin. Il problema è stato con Y! Q (ricerca correlati) plugin.
Thanks again for the catch. Grazie ancora per la cattura.
June 19th, 2005 at 5:14 am 19 giugno 2005 a 5:14 am
Even shorter: Anche più breve:
int count = new StringTokenizer(args[0]).countTokens(); int count = new StringTokenizer (args [0]). countTokens ();
or o
int count = args[0].trim().split(”\\s”).length; int count = args [0]. trim (). split ( "\ \ s"). lunghezza;
June 19th, 2005 at 10:43 am 19 giugno 2005 a 10:43 am
The regex is fine when you just deal with English, or other alphabetic languages, but when you’re dealing with Chinese or other Asian languages, your solution won’t work. La regex va bene quando si trattano solo con l'inglese o altre lingue alfabetico, ma quando sei si occupano di cinesi o di altre lingue asiatiche, la tua soluzione non funzionerà. Instead, you’d rather use Java’s locale specific String collators, in java.text.*. Invece, si preferisce usare Java's locale specifico String collators, java.text .*. It’s much cleaner to use this approach, than looking for the simplest one-liner solution for English. E 'molto più pulito di utilizzare questo approccio, quello di cercare il più semplice di una linea di soluzione per l'inglese.
December 8th, 2005 at 2:56 am 8 dicembre 2005 a 2:56 am
Dear Sir, Egregio Signore,
We are Stuckup in a code where we have to translate Indian rupees into words. Stuckup siamo in un codice in cui dobbiamo rupie tradurre in parole.
(eg 1234.53–>?????) (ad esempio 1234.53->?????)
April 13th, 2006 at 3:08 pm 13 aprile 2006 alle 3:08 pm
array to count how many numbers are eqauls array di contare quanti numeri sono eqauls
April 13th, 2006 at 3:10 pm 13 aprile 2006 alle 3:10 pm
array prgam solution to count how many numbers are equals array prgam soluzione per contare quanti numeri sono uguale
August 1st, 2007 at 5:16 am 1o agosto, 2007 at 5:16 am
how to break the string value come rompere il valore della stringa