SORU
18 Mayıs 2009, PAZARTESİ


Java çok satırlı dize

Perl gelen, eminim eksik am "işte belge" kaynak çok satırlı bir dize yaratmak demektir kodu:

$string = <<"EOF"  # create a three line string
text
text
text
EOF

Java sıfırdan çok satırlı benim ip bağlamak gibi her satıra tırnak hantal ve ARTI işaretleri var.

Ne daha iyi bir alternatif? Özellikleri bir dosyada benim dize tanımlamak?

Edit: İki cevap StringBuilder söylüyorlar.(Ekle) artı gösterim için tercih edilir. Kimseye bu kadar düşünüyorlar neden açıklayabilir? Bana daha çok tercih edilir hiç görünmüyor. Aradığım uzakta olması çok satırlı dizeleri, değil birinci sınıf bir dil yapısı, yani ben kesinlikle istemiyorum yerine birinci sınıf bir dil yapısı (dize birleştirme plus) yöntemini çağırır.

Edit: Sorumu biraz daha açıklamak için, performansı hakkında endişeli değilim. Sürdürülebilirlik ve tasarım konularında endişe ediyorum.

CEVAP
18 Mayıs 2009, PAZARTESİ


Bir çok satırlı en iyi seçenek (IMHO) dizeleri olacak, 'd birlikte. literal yapmak istiyorsun galiba Diğer seçenekleri (StringBuilder, Dize.biçim, Dize.birleşim) sadece eğer zaten bir dizi dize olsaydı. daha iyi olur

Bu göz önünde bulundurun:

String s = "It was the best of times, it was the worst of times,\n"
           "it was the age of wisdom, it was the age of foolishness,\n"
           "it was the epoch of belief, it was the epoch of incredulity,\n"
           "it was the season of Light, it was the season of Darkness,\n"
           "it was the spring of hope, it was the winter of despair,\n"
           "we had everything before us, we had nothing before us";

Yapımcı: Karşı Dize

String s = new StringBuilder()
           .append("It was the best of times, it was the worst of times,\n")
           .append("it was the age of wisdom, it was the age of foolishness,\n")
           .append("it was the epoch of belief, it was the epoch of incredulity,\n")
           .append("it was the season of Light, it was the season of Darkness,\n")
           .append("it was the spring of hope, it was the winter of despair,\n")
           .append("we had everything before us, we had nothing before us")
           .toString();

Karşı Dize.biçimi:

String s = String.format("%s\n%s\n%s\n%s\n%s\n%s"
         , "It was the best of times, it was the worst of times,"
         , "it was the age of wisdom, it was the age of foolishness,"
         , "it was the epoch of belief, it was the epoch of incredulity,"
         , "it was the season of Light, it was the season of Darkness,"
         , "it was the spring of hope, it was the winter of despair,"
         , "we had everything before us, we had nothing before us"
);

Karşı Java8 String.join():

String s = String.join("\n"
         , "It was the best of times, it was the worst of times,"
         , "it was the age of wisdom, it was the age of foolishness,"
         , "it was the epoch of belief, it was the epoch of incredulity,"
         , "it was the season of Light, it was the season of Darkness,"
         , "it was the spring of hope, it was the winter of despair,"
         , "we had everything before us, we had nothing before us"
);

Eğer belirli bir sistem için yeni satır istiyorsanız, ya System.getProperty("line.separator"), kullanın veya String.format 11 *kullanabilirsiniz.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Android Developers

    Android Deve

    9 Kasım 2007
  • Chaoticmoogle

    Chaoticmoogl

    13 ŞUBAT 2006
  • ipsy

    ipsy

    1 EKİM 2012