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‰