2013年11月18日星期一

commonly used class java String class problem

In the actual development work , the string processing is the most common programming tasks. The subject that is required program on the user input string for processing. Specific rules are as follows :
1. put the first letter of each word in uppercase .
2. between the numbers and letters with an underscore character ( _ ) to separate , making clearer
3. the middle of a word has multiple spaces is adjusted to a space .

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
This procedure how to write a program ah !
------ Solution ---------------------------------------- ----
split the input string after processing the previous character , press the last paragraph of the field with a positive figure cut .
------ Solution ---------------------------------------- ----
I feel should be analyzed using the charAt right character
------ Solution ------------------------ --------------------


input characters unsure how to analyze ? ?
------ Solution ---------------------------------------- ----

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class test {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        char[] chars =str.toCharArray();
        List<Character> clist = new ArrayList<Character>();
        
        String flagStr ="space";//space,字母litter,数字number
        for (Character c : chars){
            if (c==' ') {
                if(!flagStr.equals("space")){
                    clist.add(c);
                }
                flagStr="space";
            }
            else if (c.toString().matches("[0-9]")){
                
                if(flagStr.equals("litter")){
                    clist.add('_');
                    
                }
                else if(flagStr.equals("space")){
                    //判断空格前面是不是字母,如果是把空格换成下划线
                    if(clist.size()>=2&&clist.get(clist.size()-2).toString().matches("[a-zA-Z]")){
                        clist.remove(clist.size()-1);
                        clist.add('_');
                    }
                }
                clist.add(c);
                flagStr="number";
            } else if(c.toString().matches("[a-zA-Z]")){
                if(flagStr.equals("space")){
                    c = Character.toUpperCase(c);
                }
                else if(flagStr.equals("number")){
                    clist.add('_');
                }
                clist.add(c);
                flagStr="litter";
            }
        }
    for(Iterator<Character> it=clist.iterator();it.hasNext();){
        System.out.print(it.next().toString());
    }
    }

}
dasfwe   waeopirwe 7a6ef87ae8 sad56f4we we5sa4fsa
Dasfwe Waeopirwe_7_a_6_ef_87_ae_8 Sad_56_f_4_we We_5_sa_4_fsa

------ Solution ------------------------------ --------------
legendary regex
------ Solution ----------------- ---------------------------
okay ah , you put the regular expression can be replaced by the character comparisons . . . Landlord I believe you have the ability to modify the better. . .
------ Solution ---------------------------------------- ----
I just put characters into a string in a regular , more convenience .
You just put . toString (). matches ("[a-zA-Z]") character comparisons of these replaced on ok
------ Solution ------ --------------------------------------
first split ("" )
After the first change uppercase first letter and then the regular expression matching
------ For reference only --------------- ------------------------
you write this I have not learned ! I just learned java beginners to the common class String class , as well as the String class commonly used methods , can only use the String class methods commonly used to write it ? For example : indexOf (), and charAt () ..............
------ For reference only ------------- --------------------------
good ! Thank you !
------ For reference only -------------------------------------- -
  This reply was moderator deleted at 2012-04-05 09:09:40

------ For reference only ---------------------------------- -----

public class Main {

public static void main(String[] args){
String s="you and   me what java2005program";
//s="this is   a   99devil";
s=s.trim();
s=(char)(s.charAt(0)-32)+s.substring(1);//直接把字符串首字母变大写
s=s.replaceAll("\\s+", " ");//\\s表示   空格,回车,换行等空白符,+号表示一个或多个的意思,所以...
for(int i=0;i<s.length()-1;i++){
if(s.charAt(i)==' '&&(s.charAt(i+1)>='a'&&s.charAt(i+1)<='z')){//前面为空白符后面是小写字母
s=s.substring(0,i+1)+(char)(s.charAt(i+1)-32)+s.substring(i+2);
}else if(
//前面为字母后面为数字
((s.charAt(i) >= 'a' && s.charAt(i) <= 'z') || (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z'))&& ((s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '9'))
//前面为数字后面为字母
|| ((s.charAt(i + 1) >= 'a' && s.charAt(i + 1) <= 'z') || (s.charAt(i + 1) >= 'A' && s.charAt(i + 1) <= 'Z'))&& ((s.charAt(i) >= '0' && s.charAt(i) <= '9'))
){
s=s.substring(0,i+1)+"_"+s.substring(i+1);

}
System.out.println(s);
}
}

没有评论:

发表评论