Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中比较字符串并将结果保存在字符串数组中?_Java_Arrays_String - Fatal编程技术网

如何在java中比较字符串并将结果保存在字符串数组中?

如何在java中比较字符串并将结果保存在字符串数组中?,java,arrays,string,Java,Arrays,String,我有以下字符串数组: February_Report_files\customerst.rptdesign February_Report_files\Top10Percent.rptdesign February_Report_files\TopNPercent.rptdesign March_Report_files\by_sup_ML.rptdesign March_Report_files\chart__cwong.rptdesign March_Report_files\HTML5 C

我有以下字符串数组:

February_Report_files\customerst.rptdesign
February_Report_files\Top10Percent.rptdesign
February_Report_files\TopNPercent.rptdesign
March_Report_files\by_sup_ML.rptdesign
March_Report_files\chart__cwong.rptdesign
March_Report_files\HTML5 Chart.rptdesign
我希望根据不同的文件夹名称将报表文件(*.rptdesign)文件保存在不同的字符串数组中。例如:

result[0]="customerst.rptdesign,Top10Percent.rptdesign,TopNPercent.rptdesign"

 result[1]="by_sup_ML.rptdesign,chart__cwong.rptdesign,HTML5 Chart.rptdesign"
我怎样才能得到这个结果?你能给我一些建议吗?

使用
startsWith()

之后,您可能希望使用

fileName[i]=fileName[i].replaceFirst.("March_Report_files","");
例如:

public class StringTest {
    public static void main(String[] args){
        String text = "Hello\\World";
        if(text.startsWith("Hello\\"))
            text=text.replaceFirst("Hello\\\\","");
        System.out.println(text);
    }
}
使用
startsWith()

之后,您可能希望使用

fileName[i]=fileName[i].replaceFirst.("March_Report_files","");
例如:

public class StringTest {
    public static void main(String[] args){
        String text = "Hello\\World";
        if(text.startsWith("Hello\\"))
            text=text.replaceFirst("Hello\\\\","");
        System.out.println(text);
    }
}

下面是一个快速代码片段:

public static void main (String[] args)
{
    /* Sample Space */
    String[] strArr = new String[] {
        "February_Report_files\\customerst.rptdesign",
        "February_Report_files\\Top10Percent.rptdesign",
        "February_Report_files\\TopNPercent.rptdesign",
        "March_Report_files\\by_sup_ML.rptdesign",
        "March_Report_files\\chart__cwong.rptdesign",
        "March_Report_files\\HTML5 Chart.rptdesign",
    };
    /* Sort Sample Space */
    Arrays.sort(strArr);

    /* Initialize Result ArrayList */
    List<String> resultList = new ArrayList<>();

    /* Initialize */
    String[] strInit = strArr[0].split("\\\\");
    String prefix = strInit[0];
    StringBuilder result = new StringBuilder(strInit[1]);
    for(int i = 1; i < strArr.length; i++) {
        /* Split Using Backslash */
        String[] strSplit = strArr[i].split("\\\\");
        if(strSplit[0].equals(prefix)) {
            /* Append */
            result.append("," + strSplit[1]);
        } else {
            /* Add Result To List */
            resultList.add(result.toString());
            /* Reset Prefix, Result Strings */
            prefix = strSplit[0];
            result = new StringBuilder(strSplit[1]);
        }
    }
    /* Add Last Entry To List */
    resultList.add(result.toString());

    /* Print The Results */
    for(int i = 0; i < resultList.size(); i++) {
        System.out.println(resultList.get(i));
    }
}

下面是一个快速代码片段:

public static void main (String[] args)
{
    /* Sample Space */
    String[] strArr = new String[] {
        "February_Report_files\\customerst.rptdesign",
        "February_Report_files\\Top10Percent.rptdesign",
        "February_Report_files\\TopNPercent.rptdesign",
        "March_Report_files\\by_sup_ML.rptdesign",
        "March_Report_files\\chart__cwong.rptdesign",
        "March_Report_files\\HTML5 Chart.rptdesign",
    };
    /* Sort Sample Space */
    Arrays.sort(strArr);

    /* Initialize Result ArrayList */
    List<String> resultList = new ArrayList<>();

    /* Initialize */
    String[] strInit = strArr[0].split("\\\\");
    String prefix = strInit[0];
    StringBuilder result = new StringBuilder(strInit[1]);
    for(int i = 1; i < strArr.length; i++) {
        /* Split Using Backslash */
        String[] strSplit = strArr[i].split("\\\\");
        if(strSplit[0].equals(prefix)) {
            /* Append */
            result.append("," + strSplit[1]);
        } else {
            /* Add Result To List */
            resultList.add(result.toString());
            /* Reset Prefix, Result Strings */
            prefix = strSplit[0];
            result = new StringBuilder(strSplit[1]);
        }
    }
    /* Add Last Entry To List */
    resultList.add(result.toString());

    /* Print The Results */
    for(int i = 0; i < resultList.size(); i++) {
        System.out.println(resultList.get(i));
    }
}

下面是hashmap的实现

for(int i=0;i<strarr.length;i++){
        boolean blnExists=false;
        String key = strarr[i].substring(0,strarr[i].indexOf("\\"));
        String value=strarr[i].substring(strarr[i].indexOf("\\")+1);

        blnExists = result.containsKey(key);
        if(blnExists){
            value=result.get(key) + ","+value;
        }
        else{
            result.put(key, value);
        }
        result.put(key, value);

    }

下面是hashmap的实现

for(int i=0;i<strarr.length;i++){
        boolean blnExists=false;
        String key = strarr[i].substring(0,strarr[i].indexOf("\\"));
        String value=strarr[i].substring(strarr[i].indexOf("\\")+1);

        blnExists = result.containsKey(key);
        if(blnExists){
            value=result.get(key) + ","+value;
        }
        else{
            result.put(key, value);
        }
        result.put(key, value);

    }
for(int i=0;i