About Java processing strings

Source: Internet
Author: User

If there is a paragraph in English, such as: I am a student. My name is AAA. I like to swim, play basketball and play football. There are commas, little numbers, or other symbols (but if you encounter single quotes, such as my father ' s In the Father ' s to be alone as a word, how can you separate each word separately.

I wrote you a small example. ~ ~ 
//key is the use of regular expressions public 

class Stringtestone {public

 static void Main (string[] args) {
  String tags = "[, \\.\\s;!?] + ";//the Mark to be divided includes commas, periods, whitespace, exclamation marks, question mark ... You can add complete
  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);//split sentence according to given split tag ~ ~ for
  (int i = 0; i < re.length i + +) {
   System.out.println (i + 1) + ":" + re[i]);
}} Out put:

1:i
2:am
3:a
4:student
5:my
6:name 7:is 8:aaa
10:like
11:yes
12:i
13:am 14:sure 15:to 16:swim 17:play
18: Basketball
19:and
20:play
21:football
22:my 23:father
' s
24:football

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.