简体   繁体   中英

how to compare strings in java and save the result in string array?

I have the following string array:

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

I want save report files(*.rptdesign) file in different string arrays according to different folder names .for example :

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

 result[1]="by_sup_ML.rptdesign,chart__cwong.rptdesign,HTML5 Chart.rptdesign"

how can I get this result ? could you give me some suggestions ?

Here is a quick code snippet:

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));
    }
}

Output of this program:

customerst.rptdesign,Top10Percent.rptdesign,TopNPercent.rptdesign
by_sup_ML.rptdesign,chart__cwong.rptdesign,HTML5 Chart.rptdesign

Below is the implementation with 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);

    }

`

Use startsWith() :

if(fileName[i].startsWith("March_Report_files"))
    saveItHere(fileName[i]);
else
    saveItThere(fileName[i]);

You might want to shorten your String afterwards with

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

For example:

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM