简体   繁体   中英

get rid of square brackets in java

I am writing a program in SWI-prolog and Java.

My problem is, when i print the result from prolog in returns with [] and I don't want this.

The code for printing the results is

String t8 = "findDiseases(" + mylist + ",Diseases)."+ "\n";
Query q8 = new Query(t8);

Diagnosis_txt.append("Με τις δοθείσες πληροφορίες πάσχετε από: " +
                     "\n" + 
                     "\n" +
                     q8.oneSolution().get("Diseases"));
while (q8.hasMoreSolutions()) {
    Map<String, Term> s7 = q8.nextSolution();
    System.out.println("Answer is " + s7.get("Diseases"));          
}

And the printed results is

Answer is '[|]'(drepanocytocis, '[|]'(drepanocytocis, '[]'))

I want to get rid of this [|] and the [] . I want to print only drepanocytocis .

if you want to remove all special characters you can do something like this:

answer = answer.replaceAll("[^a-zA-Z ]+", "").trim();

update

to remove any duplicate spaces after that run, the full solution can do somthing like this:

answer.replaceAll("[^a-zA-Z ]+", " ")
    // remove duplicate spaces
    .replaceAll("[ ]([ ]+)", " ")
    // remove leading & trailing spaces
    .trim();

It can then be split on spaces to get the correct sanitized answer...

However, as @andy suggested, I recommend finding the source of the data, and building a proper data structure for it to return exactly what you want. post processing should only kinda be used for data you have no control of, or old versions, etc...

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