Domanda

Ciao a tutti Sono un nuovo arrivato qui,

Sto usando il seguente codice da una libreria open source (Matrix Toolkits per Java) che genera la seguente matrice

  1000       1000                   5
     3          5  1.000000000000e+00

Sto cercando di eseguire una suddivisione in stringhe che mi restituirà 1000.1000,5

Ho provato a usare String [] parts = str.trim (). split (" \\ s ");

ma sembra che usare \ s come token stringa sia sbagliato, hai idea di cosa dovrei usare invece?

grazie mille!

public String toString() {
        // Output into coordinate format. Indices start from 1 instead of 0
        Formatter out = new Formatter();

        out.format("%10d %10d %19d\n", numRows, numColumns, Matrices
                .cardinality(this));

        for (MatrixEntry e : this)
            if (e.get() != 0)
                out.format("%10d %10d % .12e\n", e.row() + 1, e.column() + 1, e
                        .get());

        return out.toString();
    }
È stato utile?

Soluzione

Dovresti dividere su qualsiasi numero di spazi, non solo su singoli spazi. Cioè, aggiungi " + " al tuo regexp in questo modo:

String[] parts = str.trim().split("\\s+"); 

Altri suggerimenti

StringTokenizer dovrebbe anche essere in grado di fare quello che vuoi.

String s = "  1000       1000                   5";
java.util.StringTokenizer st = new java.util.StringTokenizer(s);
while (st.hasMoreTokens()) {
    System.out.println(st.nextToken());
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top