java格式字元串
❶ java字元串反向格式化如何實現; 我們現在都是通過有佔位符的格式化字元串, 填入佔位的值, 得到結果
代碼為:
public class Main {
public static void main(String[] args) {
String format = "select %s from %s limit %s";
String param = "select * from user limit 10";
//根據%s分割format字元串
String[] stArr = format.split("%s");
//使用集合存儲結果集
List<String> result = new ArrayList<>();
for (int i = 0; i < stArr.length; i++) {
//根據分割內容裁剪param字元串
param = param.substring(stArr[i].length());
if (i < stArr.length - 1) {
//計算下一條分割內容的位置
int index = param.indexOf(stArr[i + 1]);
//截取出結果放入結果集
result.add(param.substring(0, index));
//截取後的剩餘部分重新賦值給param
param = param.substring(index);
} else {
//如果param以%s結尾,則將param剩餘部分做為最後一次結果
if (param.length() > 0) {
result.add(param);
}
}
}
System.out.println(result);
}
}
❷ JAVA中有哪幾種格式化字元串
public class test1 { //新建類
public static void main(String[] args) { //主方法
String str = String.format("%d",400/2); //將結果以十進制顯示
String str2 = String.format("%b",3 > 5); //將結果以布爾形式顯示
String str3 = String.format("%x",200); //將結果以10進制格式顯示
System.out.println("400的一半是:"+str); //輸出格式化字元串
System.out.println("3>5正確嗎:"+str2);
System.out.println("200的16進制數是:"+str3);
}
}
為什麼會報錯:
類型 String 中的方法 format(String, Object[])對於參數(String, int)不適用
類型 String 中的方法 format(String, Object[])對於參數(String, boolean)不適用
類型 String 中的方法 format(String, Object[])對於參數(String, int)不適用
❸ 怎麼 知道 java字元串 編碼格式
這樣的測試方法是不正確的。getBytes(charset)是解碼,new
String(byte[],
charset)是編碼。new
String(str.getBytes(charset),charset)是解碼再編碼,無論charset是什麼編碼格式,返回的字元串內容原始str是一致,因此equals方法都是返回true,達不到測試字元串編碼格式的目的。個人觀點:正確的測試姿勢應該是這樣的:
String charset ="xxx"; //假定編碼格式
String str = "中文";
boolean flag = str.equals(new String(str.getBytes(),charset));flag為true則表明str的編碼格式是假定的編碼格式。其中說明str.getBytes()該方法就是按其自身編碼格式去解碼。其自身編碼格式跟你的操作系統編碼格式或你使用的IDE設置的文件的Text
file
encoding有關。
❹ java 怎樣將string 格式化
在java中,將浮點數格式化為string,一般使用DecimalFormat。DecimalFormat的用法示例如下:DecimalFormatdf=newDecimalFormat();doubledata=1234.56789;System.out.println("格式化之前的數字:"+data);Stringstyle="0.0";//定義要顯示的數字的格式df.applyPattern(style);//將格式應用於格式化器System.out.println("採用style:"+style+"格式化之後:"+df.format(data));style="00000.000kg";//在格式後添加諸如單位等字元df.applyPattern(style);System.out.println("採用style:"+style+"格式化之後:"+df.format(data));//模式中的"#"表示如果該位存在字元,則顯示字元,如果不存在,則不顯示。style="##000.000kg";df.applyPattern(style);System.out.println("採用style:"+style+"格式化之後:"+df.format(data));//模式中的"-"表示輸出為負數,要放在最前面style="-000.000";df.applyPattern(style);System.out.println("採用style:"+style+"格式化之後:"+df.format(data));//模式中的","在數字中添加逗號,方便讀數字style="-0,000.0#";df.applyPattern(style);System.out.println("採用style:"+style+"格式化之後:"+df.format(data));//模式中的"E"表示輸出為指數,"E"之前的字元串是底數的格式,//"E"之後的是字元串是指數的格式style="0.00E000";df.applyPattern(style);System.out.println("採用style:"+style+"格式化之後:"+df.format(data));//模式中的"%"表示乘以100並顯示為百分數,要放在最後。style="0.00%";df.applyPattern(style);System.out.println("採用style:"+style+"格式化之後:"+df.format(data));//模式中的"\u2030"表示乘以1000並顯示為千分數,要放在最後。style="0.00\u2030";//在構造函數中設置數字格式DecimalFormatdf1=newDecimalFormat(style);//df.applyPattern(style);System.out.println("採用style:"+style+"格式化之後:"+df1.format(data));下面是總結:格式化之前的數字:1234.56789採用style:0.0格式化之後:1234.6採用style:00000.000kg格式化之後:01234.568kg採用style:##000.000kg格式化之後:1234.568kg採用style:-000.000格式化之後:-1234.568採用style:-0,000.0#格式化之後:-1,234.57採用style:0.00E000格式化之後:1.23E003採用style:0.00%格式化之後:123456.79%採用style:0.00‰格式化之後:1234567.89‰