1、在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b。
2、写一个方法去掉字符串前后空格。(主要考察代码思路,对字符串为空或NULL是否进行处理。)
   都用java实现的代码

解决方案 »

  1.   

    >>主要考察代码思路,
    不明白怎么考察
    jdk源码中关于String去掉前后空格的代码(trim()方法)是这样写的.    public String trim() {
    int len = count;
    int st = 0;
    int off = offset;      /* avoid getfield opcode */
    char[] val = value;    /* avoid getfield opcode */ while ((st < len) && (val[off + st] <= ' ')) {
        st++;
    }
    while ((st < len) && (val[off + len - 1] <= ' ')) {
        len--;
    }
    return ((st > 0) || (len < count)) ? substring(st, len) : this;
        }但估计你要是这么写,你领导能骂死你. 难道你比jdk源码里写的还好?
      

  2.   

    public final class test {
    public static void main(String[] args) {
    System.out.println(single());
    }
    public static String single(){
    StringBuffer sb = new StringBuffer();
    Scanner input = new Scanner(System.in);
    System.out.println("输入一串字母");
    if (input.hasNext()) {//这里虽然判断了是否输入即是否为null。但是scanner控制台输入我还真不知道如何触发他为false的情况
    String sth = input.next().trim();//这里获取控制台输入的字符串的时候进行了去空格处理。
    for (int i = 0; i < sth.length(); i++) {
    int count = 0;
    for (int j = 0; j < sth.length(); j++) {
    if (sth.charAt(i) == sth.charAt(j)) {
    count++;
    }
    }
    if (count == 1) {
    sb.append(sth.charAt(i));
    }
    }
    return sb.toString();
    }
    return "丫什么都没输入我给你判断什么?!";
    }
    }
    控制台输入及输出结果如下:
    输入一串字母
    abcdeefab
    cdf
      

  3.   


    第一题public class Test { /**
     * @Function:
     * @Since Oct 12, 2011
     * @param args
     */
    public static void main(String[] args) {
    String str = "abcbcefg";
    for(int i = 0 ; i < str.length();i++){
    if(str.indexOf(str.charAt(i)) == str.lastIndexOf(str.charAt(i))){
    System.out.println(str.charAt(i));
    break;
    }
    } }}
      

  4.   

    第二题不多说,看下trim()的源码就知道
        public String trim() {
    int len = count;
    int st = 0;
    int off = offset;      /* avoid getfield opcode */
    char[] val = value;    /* avoid getfield opcode */ while ((st < len) && (val[off + st] <= ' ')) {
        st++;
    }
    while ((st < len) && (val[off + len - 1] <= ' ')) {
        len--;
    }
    return ((st > 0) || (len < count)) ? substring(st, len) : this;
        }