公司网站用个人备案 2018,吉安网站建设公司,如何创建自己的网站平台,静安区网站建设在 Java 中#xff0c;字符串#xff08;String#xff09;与其他类型之间的转换是开发中的高频操作。以下是 Java 字符串一、字符串转基本数据类型1. 整数类型方法#xff1a;Integer.parseInt()#xff08;返回 int#xff09;或 Integer.valueOf()#xff08;返回 In…在 Java 中字符串String与其他类型之间的转换是开发中的高频操作。以下是Java 字符串一、字符串转基本数据类型1.整数类型方法Integer.parseInt()返回int或Integer.valueOf()返回Integer对象。示例String str 123; int num1 Integer.parseInt(str); // 基本类型 Integer num2 Integer.valueOf(str); // 包装类型注意若字符串包含非数字字符如123a会抛出NumberFormatException。2.浮点数类型方法Double.parseDouble()double或Double.valueOf()Double对象。示例String str 3.14; double num1 Double.parseDouble(str); Double num2 Double.valueOf(str);科学计数法支持支持1.23E4等格式。3.布尔类型方法Boolean.parseBoolean()返回boolean或Boolean.valueOf()返回Boolean对象。规则仅true不区分大小写返回true其他均返回false。String str TRUE; boolean flag1 Boolean.parseBoolean(str); // true Boolean flag2 Boolean.valueOf(str); // Boolean.TRUE4.字符类型方法charAt(index)获取单个字符或toCharArray()转换为字符数组。示例String str Hello; char c str.charAt(0); // H char[] chars str.toCharArray(); // [H, e, l, l, o]二、字符串转对象类型1.日期类型方法使用SimpleDateFormat解析字符串。示例String dateStr 2023-10-01; SimpleDateFormat sdf new SimpleDateFormat(yyyy-MM-dd); try { Date date sdf.parse(dateStr); } catch (ParseException e) { e.printStackTrace(); }注意SimpleDateFormat非线程安全推荐使用DateTimeFormatterJava 8。2.枚举类型方法Enum.valueOf()将字符串转为枚举实例。示例public enum Color { RED, GREEN, BLUE } String colorStr RED; Color color Color.valueOf(colorStr); // Color.RED3.自定义对象JSON 转换使用 Gson 或 Jackson 库。// 使用 Gson Gson gson new Gson(); String json {\name\:\Alice\,\age\:30}; Person person gson.fromJson(json, Person.class); // 使用 Jackson ObjectMapper mapper new ObjectMapper(); Person person2 mapper.readValue(json, Person.class);XML 转换使用 JAXB。JAXBContext context JAXBContext.newInstance(Person.class); Unmarshaller unmarshaller context.createUnmarshaller(); StringReader reader new StringReader(xmlStr); Person person (Person) unmarshaller.unmarshal(reader);三、字符串转集合类型1.List方法Arrays.asList()分割字符串为列表。String str a,b,c; ListString list Arrays.asList(str.split(,)); // [a, b, c]2.Set方法通过HashSet构造函数转换。SetString set new HashSet(Arrays.asList(str.split(,)));四、字符串转字节数组与字符数组1.字节数组方法getBytes()支持指定字符编码。String str Hello; byte[] bytes str.getBytes(StandardCharsets.UTF_8);2.字符数组方法toCharArray()。char[] chars Hello.toCharArray();五、进制转换十进制转其他进制int num 255; String hex Integer.toHexString(num); // ff String binary Integer.toBinaryString(num); // 11111111其他进制转十进制String binaryStr 1010; int decimal Integer.parseInt(binaryStr, 2); // 10六、异常处理与最佳实践异常捕获数值转换时使用try-catch处理NumberFormatException。日期解析捕获ParseException。空值检查if (str ! null !str.isEmpty()) { // 执行转换 }性能优化频繁转换时优先使用基本类型方法如parseInt()而非valueOf()。使用StringBuilder拼接字符串避免操作符的性能损耗。七、高级场景1.自定义格式转换正则表达式验证String email testexample.com; Pattern pattern Pattern.compile(^[\\w.-][\\w.-]\\.\\w$); Matcher matcher pattern.matcher(email); boolean isValid matcher.matches();Apache Commons Langimport org.apache.commons.lang3.StringUtils; String reversed StringUtils.reverse(hello); // olleh2.Base64 编码/解码示例String encoded Base64.getEncoder().encodeToString(Hello.getBytes()); byte[] decoded Base64.getDecoder().decode(encoded);总结Java 字符串转换需根据目标类型选择合适方法注意异常处理和输入验证。对于复杂场景如 JSON/XML推荐使用成熟库Gson、Jackson、JAXB。掌握这些技巧可显著提升代码健壮性和开发效率。