關於JAVA處理字串

來源:互聯網
上載者:User

如果有一段英文,如:I am a student. My name is AAA. I like to swim, play basketball and play football.這裡面有逗號、有點號,或者還有其他符號(但如果遇到單引號,如my father's 中的father's要單獨作為一個單詞),請問要怎樣才能把每個單詞單獨分開來。

 

//給你寫了小例子 自己看看吧~~ //關鍵是Regex的運用 public class StringTestOne { public static void main(String[] args) {  String tags = "[,\\.\\s;!?]+";//要分割的標記包括逗號,句號,空白符,驚嘆號,問號...你可以補充完整  String line = "I am a student. My name is AAA. I like ? Yes i am sure !  " +    " to swim, play basketball and play football.My father's football!";  String [] re = line.split(tags);//根據給定的分割標記分割句子~~  for(int i = 0; i < re.length; i ++) {   System.out.println((i + 1) + ": " + re[i]);  } }}///out put:1: I2: am3: a4: student5: My6: name7: is8: AAA9: I10: like11: Yes12: i13: am14: sure15: to16: swim17: play18: basketball19: and20: play21: football22: My23: father's24: football

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.