The simple processing of strings in the actual development work, the processing of string is the most common programming task. This topic is to require the program to the user input string processing. __ Programming

Source: Internet
Author: User
Tags stringbuffer
/* String processing is the most common programming task in the actual development work. This topic is to require the program to the user input string processing.	The specific rules are as follows: 1.
Capitalize the first letter of each word.	2. Separate the number from the letter with the underscore character (_) to make it clearer 3.
Adjust the number of spaces in the middle of the word to 1 spaces. For example: User input: You and me what Cpp2005program program output: You and me what Cpp_2005_program user input: This is a 99cat the program loses
Out: This is A 99_cat we assume that the user-entered string contains only lowercase letters, spaces, and numbers, excluding other letters or symbols.
Each word is separated by one or more spaces.
 Suppose the user enters a string that is no more than 200 characters long.
* * Import Java.util.Scanner;
Import Java.util.regex.Matcher;

Import Java.util.regex.Pattern; public class Demo02 {public static void print (string[] s) {for (int i=0;i<s.length-1;i++) {System.out.print (s[i]+
		" ");
	} System.out.println (S[s.length-1]);
		public static void Main (string[] args) {Scanner scan = new Scanner (system.in);
		String s = scan.nextline ();
		string[] ss = S.split ("[\\s]+");	for (int i=0;i<ss.length;i++) {String up = ("" +ss[i].charat (0)). toUpperCase ();
			Capital StringBuffer sb = new StringBuffer (ss[i]);
			Ss[i] = sb.replace (0, 1, up). ToString (); Matcher m = pattern.compile ("\\d+"). Matcher (Ss[i]);
				while (M.find ()) {String num = new String (M.group ());
				String num2 = num;		Num2 = "_" +num+ "_";
				Number before adding "_" ss[i] = ss[i].replace (num, num2);
				if (Ss[i].startswith ("_")) {//GO Head "_" ss[i] = ss[i].substring (1);
				} if (Ss[i].endswith ("_")) {//Go Tail "_" ss[i] = ss[i].substring (0,ss[i].length ()-1);
	}} print (ss); }
}
Run Result:
You and     me what  cpp2005program and
me what Cpp_2005_program

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.