[英]Need to put semicolon at the end of last line
我只想在第二行(或最后一行)而不是第一行的末尾放置分號。
取數組中的三個字符串: String[] = {"My first string", "My second string", "My last string"};
我目前的輸出:
My first string;
My second string;
My last string;
我需要以下形式的輸出:
My first string My second string My last string;
嘗試以這種方式做某事。
String outputString;
for(String str : yourString) {
outputString = outputString + str;
}
outputString = outputString + ";" ;
其他選擇:
有 Arrays.toString() 方法,它會將數組轉換為其內容的字符串表示形式。 然后你可以將該字符串傳遞給 System.out.println 或任何你用來打印它的東西。
例如 :
String newString = Arrays.toString(str).replace(",", " ");
newString = newString.substring(1, newString.length()-1)+";";
輸出 :
我的第一根弦 我的第二根弦 我的最后一根弦;
嘗試這個:
for(int i = 0; i < array.length; i++) {
append(array[i].append((i == array.length-1) ? ";" : ""));
}
請注意,使用三元運算符將檢查數組的最后一個元素。 使用它,您可以控制分號或空格的附加
當然可以,例如:
class Test {
public static void main(String[] args) {
String[] sarr = {"My first string", "My second string", "My last string"};
String sep = "";
for (String s: sarr) {
System.out.print (sep + s);
sep = " ";
}
System.out.println(";");
}
}
它只是輸出數組中的每個字符串,前面有一個分隔符(最初是一個空字符串,但在輸出第一個數組元素之后有一個空格)。
然后,最后,它輸出分號和換行符:
My first string My second string My last string;
試試下面..它可能會有所幫助
String[] string= {"My first string", "My second string", "My last string"};
string[string.length-1]+=";";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.