0

以下をどのようにフォーマットしますか:

これから:

1: My street
2: 1232321
3: Hello there world!
4: A really really really long word!

これに:

1:                           My street
2:                             1232321
3:                  Hello there world!
4:   A really really really long word!

String.format() とその中のランダムな文字の束と関係があると思います!

4

1 に答える 1

3

最初に文字列を分割して取得し、次にフォーマットすることができ1:ますMy Street: -

String str = "1: My street";
String str2 = "3: Hello there world!";

String[] arr = str.split("(?<=:) ");
String[] arr2 = str2.split("(?<=:) ");

System.out.printf("%s%30s\n", arr[0], arr[1]);
System.out.printf("%s%30s", arr2[0], arr2[1]);

出力 : -

1:                     My street
3:            Hello there world!

メソッドを使用してこれを行うとString#substring、2 つの部分を別々に取得できます。

System.out.printf("%s%30s\n", str.substring(0, 2), str.substring(3));
于 2012-12-01T18:41:20.063 に答える