在字符串中,找出第一个不重复的字符

public class TestInterview {
    static String str = new String("stratra");
    
    public static void main(String[] args) {
        long startTime=System.currentTimeMillis();   //获取开始时间
        System.out.println(TestInterview.findViewOneString(TestInterview.str));
        long endTime=System.currentTimeMillis(); //获取结束时间
        System.out.println("程序运行时间: "+(endTime-startTime)+"ms");
    }
    
    public static String findViewOneString(String str ){
        char[] arrChar = str.toCharArray();  //为什么没toStringArray?
        int arrChar_len = arrChar.length;
        String strOne = "";
        StringBuffer strBuf = new StringBuffer("");
        
        for (int i = 0; i < arrChar_len; i++) {
            strOne = String.valueOf(arrChar[i]);
            strBuf.append(strOne);
       //判断该字符是否已经被判断过
if(!(strBuf.indexOf(strOne)!= (strBuf.length()-1))){
         //判断该字符出现的位置到结束形成的字符串是否存在本字符
if((str.substring(i+1, arrChar_len).indexOf(strOne) == -1)){ return strOne; } } } return "没有一个不重复的字符"; } } //为什么没有toStringArray() //因为String占用空间较大,用String.valueOf(char c)可以转化为String,没有关系的
原文地址:https://www.cnblogs.com/xuyuanjia/p/5653475.html