Java algorithm--simple processing of strings

Source: Internet
Author: User

The topics are as follows:

Processing of strings
In the actual development work, the processing of strings is the most common programming task.
This topic is required to process the user input string. The specific rules are as follows:
1. Capitalize the first letter of each word.
2. Separate the numbers from the letters with the underscore character (_) to make it clearer
3. Adjust the number of spaces in the middle of a word to 1 spaces.
For example:
User input:
You and me what Cpp2005program
The program output:
You and Me what Cpp_2005_program
User input:
This is a 99cat
The program output:
This is A 99_cat
We assume that the user enters a string with only lowercase letters, spaces and numbers, and no other letters or symbols.
Separated by one or more spaces between each word.
Assume that the user enters a string that is not more than 200 characters long.

Method One:

 Public classSimple processing of strings { Public Static voidMain (string[] args) {Scanner Scanner =NewScanner (system.in); Stringstring= Scanner.nextline (); Vector<character>Vector=NewVector<character> (); for(inti =0; I <string. Length (); i++) {Vector. Add (string. CharAt (i)); }Try{intindex =0; while(Index <Vector. Size ()) {//To determine whether the first lowercase English character, is the case to operate                if(Index = =0&&Vector. ElementAt (Index) >=' A '&&Vector. ElementAt (Index) <=' Z ') {//replaces The element at the specified position in this Vector with the specified element                    Vector.Set(Index, (Char) (Vector. ElementAt (Index)-(' A '-' A '))); }Else if(Vector. ElementAt (Index-1) =="'&&Vector. ElementAt (Index) = ="') {//handling possible with multiple spaces                    Vector. Remove (index);                index--; }Else if(Vector. ElementAt (Index-1) =="'&& (Vector. ElementAt (Index) >=' A '&&Vector. ElementAt (Index) <=' Z ')) {//judgment is the character behind the space                    Vector.Set(Index, (Char) (Vector. ElementAt (Index)-(' A '-' A '))); }Else if((Vector. ElementAt (Index) >=' A '&&Vector. ElementAt (Index) <=' Z ') && (Vector. ElementAt (Index-1) >=' 0 '&&Vector. ElementAt (Index-1) <=' 9 ')) {Vector. Add (Index,' _ ');                index++; }Else if((Vector. ElementAt (Index-1) >=' A '&&Vector. ElementAt (Index-1) <=' Z ') && (Vector. ElementAt (Index) >=' 0 '&&Vector. ElementAt (Index) <=' 9 ')) {//Judging by the number                    Vector. Add (Index,' _ ');                index++;            } index++; } for(inti =0; I <Vector. Size (); i++) {System.out.print (Vector. ElementAt (i));        } System.out.println (); }Catch(ArrayIndexOutOfBoundsException e) {        }    }}

Method Two: The main use of regular expressions to intercept the string, and then each character array of elements of a regular match, containing a number of separate processing

 Public classsimplestring {///function for printing strings     Public Static void Print(string[] s) { for(inti =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 =NewScanner (System.inch);        String s = scan.nextline (); string[] ss = S.split ("[\\s]+]);//Based on regular expressions, delete one or more spaces and save the string as a character array         for(inti =0; i < ss.length; i++) {//Change the first letter of each character array to uppercaseString up = (""+ Ss[i].charat (0). toUpperCase ();//UppercaseStringBuffer SB =NewStringBuffer (Ss[i]); Ss[i] = Sb.replace (0,1, up). ToString ();the first letter of the string array is capitalized, and then the changed character array is determined if there is a numberMatcher m = Pattern.compile ("\\d+"). Matcher (Ss[i]);//0-9 appears one or more times             while(M.find ()) {//M.group (): Returns the input subsequence matched by the previous matchString num =NewString (M.Group());                String num2 = num; num2 ="_"+ num +"_";//Add "_" before and after numbersSs[i] = ss[i].replace (num, num2);if(Ss[i].startswith ("_")) {//Go to Head "_"Ss[i] = ss[i].substring (1); }if(Ss[i].endswith ("_")) {//Go to the End "_"Ss[i] = ss[i].substring (0, Ss[i].length ()-1);    }}}, print (ss); }}

Java algorithm--simple processing of strings

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.