1、String和int转换
(1)Sting 转为int
String str = "123";
try {
int a = Integer.parseInt(str);
} catch (NumberFormatException e) {
e.printStackTrace();
}
(2)int转为String
String str = "123";
try {
int b = Integer.valueOf(str).intValue()
} catch (NumberFormatException e) {
e.printStackTrace();
}
String str = "123";
int sb = String.valueOf(str);
2、Map转String
在做Map<String,Object>数据存入redis和kafka时,需要使用这样的转换:json工具包
public static String map2Json(Map<?, ?> map) {
try {
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return objectMapper.writeValueAsString(map);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
JSONObject jsonobject = JSONObject.fromObject(str);
String msg = jsonObject.toString();
3、String和byte转换
(1)string转换为byte
String str = "abcdef";
byte b[] = str .getBytes();//String转换为byte[]
(2)byte转换为string
//b为byte[]
String t = new String(b);
持续整理中......