• java简单字符串处理


    在实际的开发工作中,对字符串的处理是最常见的编程任务。

    本题目即是要求程序对用户输入的串进行处理。具体规则如下:

    1. 把每个单词的首字母变为大写。

    2. 把数字与字母之间用下划线字符(_)分开,使得更清晰

    3. 把单词中间有多个空格的调整为1个空格。

    例如:

    用户输入:

    you and     me what  cpp2005program

    则程序输出:

    You And Me What Cpp_2005_program

    用户输入:

    this is     a      99cat

    则程序输出:

    This Is A 99_cat

    我们假设:用户输入的串中只有小写字母,空格和数字,不含其它的字母或符号。

    每个单词间由1个或多个空格分隔。

    假设用户输入的串长度不超过200个字符。

     1 import java.util.Scanner;
     2 import java.util.Vector;
     3 
     4 import javax.print.Doc;
     5 
     6 
     7 public class Format {
     8     public static void main(String[] args) {
     9         Scanner scanner=new Scanner(System.in);
    10         String string=scanner.nextLine();
    11         Vector< Character> vector=new Vector<Character>();
    12         for (int i = 0; i < string.length(); i++) {
    13             vector.add(string.charAt(i));
    14         }
    15         //
    16         int index=0;
    17         while (index<vector.size()) {
    18             //把一段话的首个单词大写
    19             if (index==0&&vector.elementAt(index)>='a'&&vector.elementAt(index)<='z') {
    20                 vector.set(index,(char)(vector.elementAt(index)-('a'-'A')) );
    21             }
    22             //去除多个空格
    23             else if (vector.elementAt(index-1)==' '&&vector.elementAt(index)==' ') {
    24                 vector.remove(index);
    25                 index--;
    26             }
    27             //把段中的单词首字母大写
    28             else if (vector.elementAt(index-1)==' '&&vector.elementAt(index)>='a'&&vector.elementAt(index)<='z') {
    29                 vector.set(index, (char)(vector.elementAt(index)-('a'-'A')));
    30             }
    31             //数字后加下划线
    32             else if((vector.elementAt(index)>='a'&&vector.elementAt(index)<='z')&&(vector.elementAt(index-1)>='0'&&vector.elementAt(index-1)<='9')){
    33                 vector.add(index, '_');
    34                 index++;
    35             }
    36             //数字前加下划线
    37             else if((vector.elementAt(index-1)>='a'&&vector.elementAt(index-1)<='z')&&(vector.elementAt(index)>='0'&&vector.elementAt(index)<='9')){
    38                 vector.add(index, '_');
    39                 index++;
    40             }
    41             index++;
    42         }
    43         for (int i = 0; i <vector.size(); i++) {
    44             System.out.print(vector.elementAt(i));
    45         }
    46         System.out.println();
    47 
    48     }
    49 }

    运行结果:

    you and     me what  cpp2005program

    You And Me What Cpp_2005_program

     1 方法二:
     2 
     3 import java.util.Scanner;
     4 
     5 import java.util.regex.Matcher;
     6 
     7 import java.util.regex.Pattern;
     8 
     9 /*
    10 
    11  * 本题目即是要求程序对用户输入的串进行处理。具体规则如下:
    12 
    13  *1. 把每个单词的首字母变为大写。
    14 
    15  *2. 把数字与字母之间用下划线字符(_)分开,使得更清晰
    16 
    17  *3. 把单词中间有多个空格的调整为1个空格。
    18 
    19  *我们假设:用户输入的串中只有小写字母,空格和数字,不含其它的字母或符号。每个单词间由1个或多个空格分隔。
    20 
    21  *假设用户输入的串长度不超过200个字符。
    22 
    23  */
    24 
    25 public class SimpleString {
    26 
    27         public static void main(String args[]){
    28 
    29                  String str="";
    30 
    31                  str=(new Scanner(System.in)).nextLine();
    32 
    33                  String []str1=str.split("[ ]+");
    34 
    35                  for(int i=0;i<str1.length;i++)str1[i]=String.valueOf((char)(str1[i].charAt(0)+('A'-'a')))+str1[i].substring(1);
    36 
    37                  String s="";
    38 
    39                  for(int i=0;i<str1.length-1;i++)//System.out.print(str1[i]+" ");
    40 
    41                  {
    42 
    43                          s+=str1[i]+" ";
    44 
    45                  }
    46 
    47                  s+=str1[str1.length-1];
    48 
    49                          Pattern p=Pattern.compile("([0-9]+)");
    50 
    51                          Matcher m=p.matcher(s);
    52 
    53                          String fin="";
    54 
    55                          int st=0;
    56 
    57                          while(m.find()){
    58 
    59                                   int start=m.start();
    60 
    61                                   int end=m.end();
    62 
    63                                   fin+=s.substring(st,start);
    64 
    65                                   if(s.charAt(start-1)!=' ')fin+="_";
    66 
    67                                   fin+=m.group(1);
    68 
    69                                   if(s.charAt(end)!=' ')fin+="_";
    70 
    71                                   st=end;
    72 
    73                          }
    74 
    75                          if(st<s.length())fin+=s.substring(st);
    76 
    77                          System.out.println(fin);
    78 
    79         }
    80 
    81 }
    View Code
     1 方法三:
     2 
     3 import java.util.Scanner; 
     4 
     5 import java.util.regex.Matcher; 
     6 
     7 import java.util.regex.Pattern; 
     8 
     9  
    10 
    11 public class Demo02 { 
    12 
    13     public static void print(String[] s){ 
    14 
    15         for(int i=0;i<s.length-1;i++){ 
    16 
    17             System.out.print(s[i]+" "); 
    18 
    19         } 
    20 
    21         System.out.println(s[s.length-1]); 
    22 
    23     } 
    24 
    25     public static void main(String[] args) { 
    26 
    27         Scanner scan = new Scanner(System.in); 
    28 
    29         String s = scan.nextLine(); 
    30 
    31         String[] ss = s.split("[\s]+");  // \s表示空格,回车,换行等空白符, +号表示一个或多个的意思
    32 
    33  
    34 
    35         for(int i=0;i<ss.length;i++){ 
    36 
    37             String up = (""+ss[i].charAt(0)).toUpperCase(); // 大写  
    38 
    39             StringBuffer sb = new StringBuffer(ss[i]); 
    40 
    41             ss[i] = sb.replace(0, 1, up).toString(); 
    42 
    43             Matcher m = Pattern.compile("\d+").matcher(ss[i]); 
    44 
    45             while(m.find()){ 
    46 
    47                 String num = new String(m.group()); 
    48 
    49                 String num2 = num; 
    50 
    51                 num2 = "_"+num+"_";     // 数字前添加"_"  
    52 
    53                 ss[i] = ss[i].replace(num, num2); 
    54 
    55                 if(ss[i].startsWith("_")){  // 去头"_"  
    56 
    57                     ss[i] = ss[i].substring(1); 
    58 
    59                 } 
    60 
    61                 if(ss[i].endsWith("_")){    // 去尾"_"  
    62 
    63                     ss[i] = ss[i].substring(0,ss[i].length()-1); 
    64 
    65                 } 
    66 
    67             } 
    68 
    69         } 
    70 
    71         print(ss); 
    72 
    73     } 
    74 
    75 } 
  • 相关阅读:
    Android之剑法初略:dalvik vm和jvm比较
    人民币阿拉伯数字转换为汉字大写 code
    数据库定时备份方案及实践
    [postfix]添加黑名单
    [php][随机数]曲线式的随机
    记公司服务器维护经历
    批量修改符号链接实现思路
    复杂数组的签名生成方法
    [已解决]ubuntu下chrome和firefox输入框内无法快捷键全选
    [已解决]centos6.4 php连接mysql和memcache提示权限不允许
  • 原文地址:https://www.cnblogs.com/littlewriter/p/5778743.html
Copyright © 2020-2023  润新知