【发布时间】:2017-02-26 18:38:28
【问题描述】:
我需要创建一个字符串,该字符串成为 SHA 256 函数的输入,以生成其等效哈希值。该字符串是通过连接几个变量来创建的,如下所示:
String strRequest = "";
strRequest = request_passphrase.concat("access_code=").concat(access_code).concat("amount=").concat(amount).concat("command=").concat(mode).concat("currency=").concat(currency).concat("merchant_identifier=").concat(merchant_identifier).concat(request_passphrase);
if(strRequest!="" || !strRequest.isEmpty()) {
signature = sha256(strRequest);
}
创建 if-else 以删除 null 变量的串联的最佳方法应该是什么。
例如。如果 access_code 为 Null 或为空,则字符串将为 request_passphrase.concat("amount=").concat(amount)。等等。
【问题讨论】:
-
你听说过
Map吗?在这里会很有用。附言您似乎正在构建一个查询字符串。搜索该词会找到很多。 -
注意:
strRequest!="" || !strRequest.isEmpty()始终为真,给定上一行。而strRequest != ""几乎肯定不会检查你认为它做了什么。 -
谢谢安迪。现在已经改正了。现在使用 && 而不是 OR 并使用 strRequest ! = null。
-
感谢您的快速投票!
标签: java if-statement null concat string-concatenation