Simplifying Java Software Development: How to Count the Number of Words in a String using Java (one-liner) Javaソフトウェアの開発を簡素化:数をカウントする方法の数を使用して文字列の単語をジャワ( 1つ-ライナー)
Java is a language of choice for millions of developers worldwide. Javaは、言語の選択を何百万もの開発者の世界です。 In a series of articles I will show simple tips and techniques which make Java extremely powerful and yet simple to use.で、一連の記事が表示さ簡単なヒントやテクニックを私にJavaの非常に強力ではまだありませんが、簡単に使用します。 Today’s article is about using regex, a pattern matcher incorporated in Java (from 1.4 I believe).今日の記事は正規表現を使用して、 Javaのパターンが組み込まれてmatcher (より1.4と思う) 。
Here is a sample code (line in bold) to count the number of words in any amount of text.次に示すのは、サンプルコード(太字で表示さ行)の単語数をカウントするためにどれだけの量のテキストです。 The sample program counts the number of words in the argument to the program.サンプルプログラムの数を数えるの単語を引数としてのプログラムです。 The argument must be quoted to ensure separate words are clubbed together in a single sentence by the operating system.引数を確保するための別の言葉を引用する必要がありますが、 1つの文章をこん棒で一緒には、オペレーティングシステムです。
public class WordCount { public static void main(String args[]) { System.out.println(java.util.regex.Pattern.compile(”[\\w]+”).split(args[0].trim()).length); } }パブリッククラスワードカウント(公共の静的な無効メイン(文字列args [ ] ) ( system.out.println ( java.util.regex.pattern.compile ( " [ \ \ワット]+").分割( args [ 0 ] 。トリム( ) ) 。長さ) ; ) ) Sample usage:サンプルの利用方法:
java WordCount “This is a sample phrase with 8 words” ジャワワードカウント"これは、サンプルのフレーズを8ワード"
All its does is compile the regular expression “\w” which matches with words and splits the phrase in an array of individual words.すべての機能は、正規表現をコンパイルして" \ w "のどの試合を単語やフレーズを分割し、個々の単語の配列をします。 Then the total length of the array is printed.の合計の長さを入力し、その配列が印刷されます。
Filed under提出されて Headline Newsニュースの見出し , 、 How Toどのように , 、 Java Software Javaソフトウェア | |
| |
RSS 2.0 RSS 2.0を | |
Email this Article電子メールこの記事
You may also like to readを読むようにすることも可能 |





June 19th, 2005 at 12:02 am 2005年6月19日の12:02時
Come on!ちょっと待って! There was absolutely no content in the feed.があったのコンテンツを全くフィードのです。 Not even a teaser.お誘いもないです。 Just ads.広告だけです。
Can you point me to one of your feeds with some content (and preferably no ads), or would you rather me unsubscribe?私にできるの1つをポイントしてフィードをいくつかのコンテンツ(とできれば広告が表示さ) 、またはようにむしろ私退会ですか?
June 19th, 2005 at 12:16 am 2005年6月19日の午前7:16
That’s is very surprising.それは非常に驚くべきことです。 Thanks for pointing it out.ポインティングしていただきありがとうございます。 It was working well before.前はうまく動作しています。 I tested it several times.私に何回かテストします。
I am working to debug the problem.私はこの問題をデバッグする作業です。 Can you tell me approximately from when the feed started spewing only ads?約からのことを教えてかんしゃくを始めたときに、フィード広告のみですか?
BTW: I stopped the Ad engine for now, till I rectify the problem.ところで:私の広告エンジンを停止今のところは、問題を是正するまで私です。 However it seems the problem is elsewhere.しかしこの問題は、他のようです。 The actual feed content is not getting posted in feed.就学許可証の実際のフィードのコンテンツに投稿されたフィードではない。
June 19th, 2005 at 12:22 am 2005年6月19日の午前7:22
Here is what I got:ここで私は何:
Sponsor(s):スポンサー(敬称略) :
Tired of dealing with messy toner, lost fax pages and unreliable fax machines?メッシー疲れへの対処法トナー、失われた信頼できないページやファクシミリファックスですか? Why pour more money into extra phone lines and machine maintenance when you can send and receive faxes by email?なぜより多くの資金を投入するときに余分な電話回線や機械のメンテナンスをすることができます。ファクス電子メールの送受信ですか? eFax is the #1 paperless faxing choice of individuals and businesses worldwide. efaxは、 # 1ペーパーレスファクスワールドワイド個人や企業の選択です。 Get a local fax number to send and receive faxes right from your email inbox.取得するローカルファックス番号を送信する権利やファクスを受信してメールの受信トレイからです。 It’s the smart way to access and organize your faxed documents.それはスマートな方法にアクセスしてファクスで送られた書類を整理しています。 Try eFax Plus free for a month. efaxプラス1カ月間無料で試しています。
You are on the phone with your client, going through an RFP that you emailed them.あなたがその場で電話をしてクライアントを、電子メール経由でrfpことです。 You hear them flip through the pages.ページをパラパラめくることを聞いています。 Suddenly, they ask: “How much will this cost?” This happens every day, right?突然、かれらは言う: "はどのくらいかかりますか? "これは毎日、正しいですか? Not with GoToMeeting, the easiest way to hold online meetings. gotomeetingではなく、オンラインミーティングを保持する最も簡単な方法です。 You can present, share, and field questions - right from your desk.することができます。現在、共有、およびフィールドの質問-右からお客様のデスクです。 So you stop the “skip ahead syndrome” before it starts.これを停止する"に進ん症候群"前に開始されます。 Try it FREE now.今すぐ無料でお試しください。
© 2005 blog.taragana.com. © 2005 blog.taragana.comです。 This RSS Feed is for personal non-commercial use only.このRSSフィードは、個人的な非営利目的でのみ使用します。 If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement.読書ていない場合は、この資料で、ニュースアグリゲータ、そのサイトを見ては、あなたが著作権侵害として有罪を宣告した。 Please contactお問い合わせ legal@taragana.com so we can take legal action immediately.ので、すぐに法的措置を取ることです。
June 19th, 2005 at 12:29 am 2005年6月19日12:29アム
Can you please tell me from when it started happening?ことを教えてください始めたときに起きてからですか?
I saw the problem with the main feed and some other feeds like technology and web feeds.を見て、問題は、メインのフィードやフィードのようないくつかの他の技術とWebフィードを使用できます。
However diabetes feed seems to be ok.しかし糖尿病のフィードと思われるします。
Looks like a problem with some characters in some posts.次のように問題があるいくつかの記事をいくつかの文字です。 I am trying to find it.私はしようとして見つけることです。 Debugging PHP is such a royal pain!ロイヤルPHPのデバッグは、このような痛み!
June 19th, 2005 at 12:33 am 2005年6月19日の12:33時
I think it started with this one:と思うと、この1つの開始:
http://blog.taragana.com/index.php/archive/what-is-your-favorite-programming-language/
June 19th, 2005 at 12:52 am 2005年6月19日の12:52時
Thanks.ありがとう。
Can you please try it again and let me know if the problem is fixed on your end.ことをお試しくださいしてくださいをご存知でしたら、私の場合、問題が修正さを知ってして終了します。 You will have to refresh the feed.フィードを更新する必要があります。 You can use any feed url.フィードのURLを使用することができます。
I have tested here on two offline feed aggregators.私はここに2つのオフラインフィードアグリゲータのテストです。
The problem was with Y!Q (Related Search) plugin.この問題は、とY !問(関連する検索サイト)差込式です。
Thanks again for the catch.感謝再びをキャッチします。
June 19th, 2005 at 5:14 am 2005年6月19日5:14アム
Even shorter:さらに短く:
int count = new StringTokenizer(args[0]).countTokens(); int数=新しいstringtokenizer ( args [ 0 ] ) 。 counttokens ( ) ;
orまたは
int count = args[0].trim().split(”\\s”).length; int数= args [ 0 ] 。トリム( ) 。スプリット( " \ \秒" ) 。長さ;
June 19th, 2005 at 10:43 am 2005年6月19日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.正規表現は素晴らしいだけに対処するときに英語、またはその他のアルファベット言語を扱うときはしかし、中国語や他のアジア言語は、あなたのソリューションは動作しません。 Instead, you’d rather use Java’s locale specific String collators, in java.text.*.その代わりに、 Javaのロケールを使用するのにはむしろ特定の文字列にcollatorsは、 java.text .*. It’s much cleaner to use this approach, than looking for the simplest one-liner solution for English.クリーナーの方がずっとこのアプローチを使用するよりも、ライナーを探して1つのソリューションを提供する最も簡単な英語です。
December 8th, 2005 at 2:56 am 2005年12月8日は2:56アム
Dear Sir,拝啓、
We are Stuckup in a code where we have to translate Indian rupees into words.我々は我々が抱えてstuckupにするコードを翻訳するインドのルピーを言葉にします。
(eg 1234.53–>?????) (例: 1234.53 - >?????)
April 13th, 2006 at 3:08 pm 2006年4月13日は3:08 pmの
array to count how many numbers are eqaulsの配列を数多くの数字がどのようeqauls
April 13th, 2006 at 3:10 pm 2006年4月13日は午後3時10分
array prgam solution to count how many numbers are equals配列prgamの解決策を数多くの数字がどのように等しい
August 1st, 2007 at 5:16 am 2007年8月1日5:16アム
how to break the string value文字列値を打破する方法について