Debbie Mp
Debbie Mp

Reputation: 163

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.

Upvotes: 0

Views: 353

Answers (1)

epoch
epoch

Reputation: 16605

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...

Upvotes: 2

Related Questions