欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Java算法之串的简单处理

时间:2021-05-27 08:46:08|栏目: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个字符。

方法一:

public class 串的简单处理 {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    String string = scanner.nextLine();
    Vector<Character> vector = new Vector<Character>();
    for (int i = 0; i < string.length(); i++) {
      vector.add(string.charAt(i));
    }
    try {
      int index = 0;
      while (index < vector.size()) {
        //判断第一个是否为小写的英文字符,是的话进行操作
        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) == ' ') {
          //处理有多个空格的可能
          vector.remove(index);
          index--;
        } else if (vector.elementAt(index - 1) == ' '
            && (vector.elementAt(index) >= 'a' && vector
                .elementAt(index) <= 'z')) {
          //判断是空格后边的字符
          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')) {
          //判断的是数字
          vector.add(index, '_');
          index++;
        }
        index++;
      }
      for (int i = 0; i < vector.size(); i++) {
        System.out.print(vector.elementAt(i));
      }
      System.out.println();
    } catch (ArrayIndexOutOfBoundsException e) {
    }
  }
}

方法二:主要用到正则表达式对字符串进行截取,然后对每一个字符数组的元素进行正则匹配,含有数字的单独进行处理

public class SimpleString {
  // 打印字符串的函数
  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(); // 大写
      StringBuffer sb = new StringBuffer(ss[i]);
      ss[i] = sb.replace(0, 1, up).toString();
      // 上边已经把字符串数组的首字母该为大写,然后对更改后的字符数组判断是否有数字
      Matcher m = Pattern.compile("\\d+").matcher(ss[i]);// 0-9出现一次或多次
      while (m.find()) {
        // m.group():Returns the input subsequence matched by the previous match
        String num = new String(m.group());
        String num2 = num;
        num2 = "_" + num + "_"; // 数字前后都添加"_"
        ss[i] = ss[i].replace(num, num2);
        if (ss[i].startsWith("_")) { // 去头"_"
          ss[i] = ss[i].substring(1);
        }
        if (ss[i].endsWith("_")) { // 去尾"_"
          ss[i] = ss[i].substring(0, ss[i].length() - 1);
        }
      }
    }
    print(ss);
  }
}

总结

上一篇:spring web.xml指定配置文件过程解析

栏    目:JAVA代码

下一篇:Java钩子方法概念原理详解

本文标题:Java算法之串的简单处理

本文地址:http://www.codeinn.net/misctech/130160.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有