0

I have a string like this format

Number. Subject, Uni Name, Location 2

I want to re-order the format like this one

Number. Uni Name, Subject, Location 2

I can do the operation by this way

int index = Order .indexOf(",");
String newStr =Order .substring(index + 1, Order .length());

But,I think all string have a different name and length, so may be this will not work. So, my question is how can re-order the string format dynamically with a efficient way ?

Question updated

user2579475
  • 1,051
  • 3
  • 11
  • 20
  • 1
    Use the `split` method and concatenate the strings in the resulting array in any way you want. – toniedzwiedz Sep 19 '13 at 15:48
  • 1
    You could split the string by comma. After that you can order the array and rebuild the string. But i'm not sure it would be so efficient – BackSlash Sep 19 '13 at 15:48
  • Are there any commas in the fields? Are there commas escaped in some way? We need a little more information... – Boris the Spider Sep 19 '13 at 15:49
  • @BoristheSpider Yes, there are three commas – user2579475 Sep 19 '13 at 15:50
  • Do `s.split(",\\s*", 3)`. This removes whitespace after the comma, and if there are more than 3 commas just three array elements remain (nothing gets lost). – Joop Eggen Sep 19 '13 at 15:55
  • 1
    You might want to look into [CSV parsing in Java](http://stackoverflow.com/questions/843997/csv-parsing-in-java-working-example). CSV stands for "Comma separated values", and there are libraries that make dealing with them easy and avoid ambiguity around commas in value fields. – Mike Samuel Sep 19 '13 at 16:06

7 Answers7

0
    String[] str = Order.split(",");

    System.out.println(str[1] +","+str[0]+","+str[2]);
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
0

you need to use String#split() function.Here you can find a good example.
So you can solve your problem like this

String[] str = Order.split(",");
String newOrderedString = str[1] +","+str[0]+","+str[2];
//OR
newOrderedString = str[1] +","+str[2]+","+str[0];
//OR
newOrderedString = str[2] +","+str[0]+","+str[1]; // As you want
Freak
  • 6,786
  • 5
  • 36
  • 54
0

Something like this could work:

String s = "Subject, Uni Name, Location 2";
String[] sp = s.split(",");
String finalString = sp[1] + "," + sp[0] + "," + sp[2];

You may consider using a StringBuilder to do the finalString composition.

Averroes
  • 4,168
  • 6
  • 50
  • 63
0

Try

 String original = "Subject, Uni Name, Location 2";
 String[] split = original.split(",");
 StringBuilder b= new StringBuilder();
 b.append(split[1]).append(",");
 b.append(split[0]).append(",");
 b.append(split[2]);
 System.out.println(b.toString());  // Uni Name,Subject, Location 2
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

Use:

String s = "Subject, Uni Name, Location 2";
String[] strs = s.split(",");

Then join the results in whatever order you need (I've used trim() to get rid of the extra spaces and added them back in where needed):

String t = strs[1].trim() + ", " + strs[0].trim() + ", " + strs[2].trim();

If you are doing this a lot (e.g. in a loop) use a StringBuilder to build the resulting string - it is much more efficient (and idiomatic).

Paul
  • 3,009
  • 16
  • 33
0

One way would be regex. This probably isn't the most efficient way but it's fairly clean:

public static void main(String args[]) {
    final Pattern pattern = Pattern.compile("(\\d++)\\.([^,]++),\\s*+([^,]++),\\s*+(.*+)");
    final Matcher matcher = pattern.matcher("");
    //for each input string
    final String input = "Subject, Uni Name, Location 2";
    matcher.reset(input);
    final String output = matcher.replaceAll("$1, $3, $2, $4");
    System.out.println(output);
}

So you pre-compile your Pattern and reuse the Matcher for maximum speed. You then simply do a replaceAll on each input String.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
0

I would say don't do this with raw Strings like this. Use an Object.

class ReportHeader {
    String number;
    String name;
    String subject;
    String location;

    ReportHeader(number,name,subject,location) {
        this.number = number;
        this.name = name;
        this.subject = subject;
        this.location = location;
    }

}

class interface HeaderFormat {
    String toString(ReportHeader header);
}

// this is how Wilson likes his reports
class ProfessorWilsonsHeaderFormat implements HeaderFormat { 
    public String toString(ReportHeader header) {
        return header.number + "," + header.name + "," + header.subject + "," + header.location;
    }
}

// this is how Thomspon likes his reports
class ProfessorThompsonsHeaderFormat implements HeaderFormat { 
    public String toString(ReportHeader header) {
        return header.number + "," + header.subject + "," + header.name + "," + header.location;
    }
}
corsiKa
  • 81,495
  • 25
  • 153
  • 204