如何将String对象转换为Boolean对象?
本文翻译自:How to convert String object to Boolean Object?
如何将String
对象转换为Boolean
对象?
#1楼
参考:https://stackoom.com/question/6SId/如何将String对象转换为Boolean对象
#2楼
Try (depending on what result type you want): 尝试(取决于您想要的结果类型):
Boolean boolean1 = Boolean.valueOf("true");
boolean boolean2 = Boolean.parseBoolean("true");
Advantage: 优点:
- Boolean: this does not create new instances of Boolean, so performance is better (and less garbage-collection). Boolean:这不会创建Boolean的新实例,因此性能更好(并且垃圾收集更少)。 It reuses the two instances of either
Boolean.TRUE
orBoolean.FALSE
. 它重用了Boolean.TRUE
或Boolean.FALSE
这两个实例。 - boolean: no instance is needed, you use the primitive type. boolean:不需要实例,使用基本类型。
The official documentation is in the Javadoc . 官方文档在Javadoc中 。
UPDATED: 更新:
Autoboxing could also be used, but it has a performance cost. 也可以使用自动装箱,但它具有性能成本。
I suggest to use it only when you would have to cast yourself, not when the cast is avoidable. 我建议只在你必须施展自己的时候使用它,而不是在施放是可以避免的时候。
#3楼
Boolean b = Boolean.valueOf(string);
如果字符串不是null并且等于true
(忽略大小写),则b
值为true。
#4楼
You have to be carefull when using Boolean.valueOf(string) or Boolean.parseBoolean(string) . 使用Boolean.valueOf(string)或Boolean.parseBoolean(string)时必须小心。 The reason for this is that the methods will always return false if the String is not equal to "true" (the case is ignored). 原因是如果String不等于“true”,则方法将始终返回false(忽略大小写)。
For example: 例如:
Boolean.valueOf("YES") -> false
Because of that behaviour I would recommend to add some mechanism to ensure that the string which should be translated to a Boolean follows a specified format. 由于这种行为,我建议添加一些机制,以确保应转换为布尔值的字符串遵循指定的格式。
For instance: 例如:
if (string.equalsIgnoreCase("true") || string.equalsIgnoreCase("false")) {Boolean.valueOf(string)// do something
} else {// throw some exception
}
#5楼
Beside the excellent answer of KLE, we can also make something more flexible: 除了KLE的优秀答案之外,我们还可以提供更灵活的内容:
boolean b = string.equalsIgnoreCase("true") || string.equalsIgnoreCase("t") || string.equalsIgnoreCase("yes") || string.equalsIgnoreCase("y") || string.equalsIgnoreCase("sure") || string.equalsIgnoreCase("aye") || string.equalsIgnoreCase("oui") || string.equalsIgnoreCase("vrai");
(inspired by zlajo's answer... :-)) (灵感来自zlajo的回答...... :-))
#6楼
boolean b = string.equalsIgnoreCase("true");
如何将String对象转换为Boolean对象?相关推荐
- js中string字符串转换为JSON对象 - 方法大全(4种)
js中string字符串转换为JSON对象 - 方法大全(4种) jQuery插件支持的转换方式: 示例: //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对 ...
- String字符串转换为实体对象
将一个String字符串转换为实体对象 LibraryGdsFaxingCatgDTO类 public class LibraryGdsFaxingCatgDTO {/*** 层级展示*/privat ...
- Java对象转换为Json对象
前言 曾几何时,XML是程序员的宠儿,是数据传输.API.AJAX应用等方面的不二选择,但自从JSON横空出世后,或者你会发觉你身边就有很多人开始抛弃XML,在他们的心目中,JSON已经完全取代了XM ...
- JavaScript(四)——具体对象(Math、字符串对象、Date对象、Number对象及Boolean对象)
文章目录 1.字符串对象 (1)转义字符 \ (2)API 2.Math对象 3.Date对象 (1)创建 (2)获取存储的日期时间 (3)转为本地字符串格式 (4)设置日期时间 (5)拷贝Date对 ...
- Java8 stream().map()将对象转换为其他对象
Java8 stream().map()将对象转换为其他对象 1: 将对象List转为List public class user{private String name;private String ...
- 对Spring Data JPA中的page对象下的content属性里的实体类对象转换为dto对象
对Spring Data JPA中的page对象下的content属性里的实体类对象转换为dto对象. 刚开始试遍历content,进行转换,添加到新的list中,再set进去page.后来发现pag ...
- 前端:JS/23/JS内置对象(String对象,Array对象,Date对象,Boolean对象,Number对象,Math对象),实例:求圆的面积,求直角三角形
JS内置对象 1,String对象 字符串对象,提供了对字符串进行操作的属性和方法 2,Array对象 数组对象,提供了数组操作方面的属性和方法 3,Date对象 日期时间对象,可以获取系统的日期时间 ...
- JavaScript内置对象→对象、系统函数、Date日期对象、String字符串对象、Math对象、Number数字对象、Object对象、Boolean对象、Error对象
对象 系统函数 Date日期对象 String字符串对象 Math对象 Number数字对象 Object对象 Boolean对象 Error对象
- java将a对象转换为b对象_Java 对象的深复制五种方式
1. 概述 在实际编程过程中,我们常常要遇到这种情况:有一个对象A,在某一时刻A中已经包含了一些有效值,此时可能 会需要一个和A完全相同新对象B,并且此后对B任何改动都不会影响到A中的值,也就是说,A ...
最新文章
- 小程序统一服务消息_[miniblog]小程序订阅消息踩坑记
- 理解深度学习中的学习率及多种选择策略
- ALV中动态内表+行转化为列
- [Linux基础环境/软件]Linux下安装resin web服务器(涉及gcc、jdk环境部署)
- mysql-odbc的zip安装方法_win10环境下mysql-odbc的zip安装方法
- RT-Thread Nano版本和RT-Thread Master版本的关系
- 【英语学习】【Level 08】U01 Let's Read L4 Hot off the press
- 股票历史信息怎么看?
- 涨薪慢,该不该跳槽?
- 【将Cityscape和Foggy_Cityscape转换为PASACAL VOC格式的目标检测数据集】
- wps怎么把当前页面设置为横向_wps单页面方向设置为横向的方法
- thymeleaf模板引擎即时生效的问题
- 解决echarts的title和legend重合问题(转)
- highcharts制作3D饼图渐变效果
- DoraCloud for Proxmox桌面云上启用NVIDIA Tesla P4的vGPU功能
- 禁用笔记本触摸板功能
- 【游戏开发实战】Unity UGUI Text图文混排(聊天文字混表情),支持动态表情,出招吧表情帝
- Python项目对接CAS方案
- 用计算机发现计算规律,用计算器探索规律
- 图卷积网络、图神经网络必读论文
热门文章
- Java 多线程编程之 notify notifyAll wait lock unlock 算法
- 算法--------旋转数组
- Java Thread.join()详解
- MySQL8.0启动服务的代码_MySQL8.0服务启动(windows10)
- python flask 表单数据输出_将数据从HTML表单发送到Flask中的Python脚本
- sqlserver怎么查看索引_Sql Server之旅——第六站 为什么都说状态少的字段不能建索引...
- 递归删除N天前的文件夹及子文件夹下的特定文件
- JMeter测试TCP/IP Socket应用的性能
- FFmpeg从入门到出家(HEVC在RTMP中的扩展)
- sublime福音:微信小程序组件及API补全插件