Java Questions
Java Questions

Reputation: 7953

adding pipe to values in java

I have some values which I have to bring them as pipe| separated and I have done like this :

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

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

this gives me what I want like the below :

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

but it is having the | at the beginning of the value I want. how to I avoid this and is there any other best way to do this ?

Please suggest me if there is.

thanks for your valuable time.

Upvotes: 1

Views: 2306

Answers (7)

Ashish Jagtap
Ashish Jagtap

Reputation: 2819

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

hope this will solve your problem...

Upvotes: 1

Sandeep Kumar
Sandeep Kumar

Reputation: 1

List transactionReferenceInfoList = paymentInfoDao.getTransactionRefNumberToVerify("0");

    for (String transactionRefNumber : transactionReferenceInfoList) {
        transactionID = transactionID + "|"+transactionRefNumber; //|XXX|XXX
    }
var trasactionID_new =transactionID.substring(1);//XXX|XXX

Upvotes: -1

Suresh Atta
Suresh Atta

Reputation: 122018

If you are sure that each time that | is there,

Without any Utils String class alone do that job with help of subString method

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

For ex:

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

Upvotes: 1

ZaoTaoBao
ZaoTaoBao

Reputation: 2615

maybe is strange answer but for making different change loop:

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

        for (String transactionRefNumber : transactionReferenceInfoList) {

           transactionID+= transactionRefNumber+"|";

        }

and if you want delete the last "|"

if i don't understand your answer, i hope apologize me.

Upvotes: 1

Rohan
Rohan

Reputation: 3078

For first Iteration check if transactionID is null.

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

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

Upvotes: 1

maximede
maximede

Reputation: 1823

I would use StringUtils.join from Apache Commons

StringUtils.join(transactionReferenceInfoList,'|');

Upvotes: 1

Mustafa Gen&#231;
Mustafa Gen&#231;

Reputation: 2579

One of the best practice is using Apache Commons Lang:

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

Or you may just put an if clause inside loop.

Upvotes: 2

Related Questions