【问题标题】:adding pipe to values in java在java中将管道添加到值
【发布时间】:2013-09-28 01:02:45
【问题描述】:

我有一些值,我必须将它们作为 pipe| 分开,我已经这样做了:

List<String> transactionReferenceInfoList = paymentInfoDao.getTransactionRefNumberToVerify("0");

        for (String transactionRefNumber : transactionReferenceInfoList) {
            transactionID = transactionID + "|"+transactionRefNumber;
        }

这给了我想要的东西,如下所示:

|6503939|2298597|4786967|2855035|8999941|7331957|1829429|7148599

但它在我想要的值的开头有|。如何避免这种情况,还有其他最好的方法吗?

如果有请给我建议。

感谢您宝贵的时间。

【问题讨论】:

  • transactionID.replaceFirst("[|]", "");
  • newuser,这太慢了。最快的解决方案是:transactionID = transactionID.substring(1);

标签: java for-loop concatenation pipe


【解决方案1】:

最佳实践之一是使用 Apache Commons Lang:

String transactionID = StringUtils.join(transactionReferenceInfoList , "|");

或者你可以在循环中放一个 if 子句。

【讨论】:

  • 或者从索引 1 的结果子串 ;-)
  • @Mustafa Genc :) 谢谢,它帮助了我,将在几分钟内将您的答案标记为已接受
  • 使用for each loopApache Commons Lang有什么区别,不介意请分享
  • 实际上 commons lang 几乎做同样的事情。它是空安全的。它消除了这些简单工作出错的可能性。如果您想知道性能,只需测量它。
  • 不要重新发明轮子!做吗?
【解决方案2】:

我会使用来自 Apache Commons 的 StringUtils.join

StringUtils.join(transactionReferenceInfoList,'|');

【讨论】:

    【解决方案3】:

    第一次迭代检查transactionID是否为空。

    List<String> transactionReferenceInfoList = paymentInfoDao.getTransactionRefNumberToVerify("0");
    
            for (String transactionRefNumber : transactionReferenceInfoList) {
                if(transactionID==null)
                      transactionID=transactionRefNumber;
                transactionID = transactionID + "|"+transactionRefNumber;
            }
    

    【讨论】:

    • 每次都会检查条件。需要与否
    • @R.S 这也是好的之一,但我更喜欢使用StringUtils.join from Apache Commons。 +1
    • @newuser 是的,这种情况在每次迭代中都会得到检查。
    • 如果列表大小为 1000 则此条件将检查 1000 次。它只需要从头开始。为什么一直如此
    【解决方案4】:

    也许是一个奇怪的答案,但为了做出不同的改变循环:

    List<String> transactionReferenceInfoList = paymentInfoDao.getTransactionRefNumberToVerify("0");
    
            for (String transactionRefNumber : transactionReferenceInfoList) {
    
               transactionID+= transactionRefNumber+"|";
    
            }
    

    如果你想删除最后一个“|”

    如果我不明白你的回答,我希望向我道歉。

    【讨论】:

    • 淘宝,:) 感谢您的宝贵时间... +1 为我花费您的时间
    【解决方案5】:

    如果您确定每次| 都在那里,

    没有任何Utils String 类单独在subString 方法的帮助下完成这项工作

    resultString = resultString.substring(1,resultString.length()); 
    

    例如:

    String resultString  = "test";
    resultString = resultString.substring(1,resultString.length());
    System.out.println(resultString);  //gives "est"
    

    【讨论】:

    • @Suresh 感谢您的宝贵时间...+1 为我花费您的时间
    【解决方案6】:
    List<String> transactionReferenceInfoList = paymentInfoDao.getTransactionRefNumberToVerify("0");
    int i = 0;
            for (String transactionRefNumber : transactionReferenceInfoList) {
            if(i==0) {
                        transactionID = transactionID + transactionRefNumber;
            } else {
                transactionID = transactionID + "|"+transactionRefNumber;
            }
            i++;
            }
    

    希望这能解决您的问题...

    【讨论】:

    • 感谢您的宝贵时间...+1 为我花费您的时间
    猜你喜欢
    • 2017-02-28
    • 2016-01-14
    • 2019-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    相关资源
    最近更新 更多