String concatenation questions

Thank you so much for ur instant reply…I appreciate it.
Thank you

Stepheny,

As you probably noticed from going through this thread, there are different ways that you can accomplish concatenation and you will find that different people will defend different approaches based on factors like readability, performance, maintanance, etc.

I, for one, believe in trying to stick as much as possible to existing buit-in services rather than creating new services. Why?
(1) because it’s one less service that I need to worry about maintaining
(2) because it keeps my folders and packages cleaner
(3) because what is more likely to be true: for a new developer to understand how to use my service OR for him/her to know how to use a built-in service that exists and is documented in every IS installation?
(4) similarly, what is more likely to be bug free: a service that is fully tested by webMethods (I hope) and is used in several IS environments OR a service that you/me/whoever creates for a single integration environment?

Having said that, here are MY PREFERENCES:

  1. For concatenating 2 strings: use pub.string:concat
  2. For concatenating 2 strings with a delimiter in the middle or 3+ strings with or without delimiters: use pub.string:makeString
  3. For concatenating multiple strings in a more complex format: use pub.string:messageFormat

Good luck!

  • Percio

All,

I noticed in this entire thread, no one suggested that concatenation can also be done via variable substitution. Ie. setting the value of a string variable to %strVar1%%strVar2%%strVar3%, for example, then checking the “Perform Variable substitution” box.

I am NOT saying this is the way to go, but I figured I’d bring it up just to see what you guys thought of this approach.

  • Percio

Thankyou so much for ur response, I will do this.
Bye and have a gr8 day

Percio, a very good point. We can even do the string concatination using variable substitution. This even reduces the steps in the service.
Jay.

Stepheny,

I meant to tell you: for your scenario I’d use pub.string:makeString and map string1 to elementList[0], string2 to elementList[1], and set separator to +++. Please note that this will only place +++ after string1 if string2 exists, and vice-versa. I’m not sure if you always want the +++ to appear or not. Let me know if you do. :wink:

  • Percio

Percio,

Thanks for jumping in…

Ofcourse we can do using with variable substitution but what if the string has null or empty value,so sometmes to avoid this risk we have to go with conditional mapping and use built-in or custom services for a successful map.

Please ignore if my statement is not valid.

Also with 2 concat services or makeString can do the job in this case.

PS:PSUtilities package also provides multiConcat java service,which serves the purpose.

HTH,
RMG

Thankyou for ur suggestions.

RMG,

I was not suggesting that variable substitution was the way to go. I was just bringing it up because I was surprised I hadn’t seen anyone mention it in this long, long thread. Personally, however, I greatly prefer the use of built-in services such as concat, makeString, and messageFormat, as indicated in my initial post.

  • Percio

Thanks Percio,got it.
Actually variable substitution has some disadv as metioned above,so i thought its not worth in this muticoncat case and avoided posting it initially.

RMG.

WOWWWWWW…This is really great. I tried it and worked and its so simple and just one step.Thank you for ur suggestions.
Have a gr8 day.

Glad to know it worked.

HTH,
RMG

Actually i can put any operator i want to.
Just something in between 2 strings. Might be +++ or — or anything.
So in in place of string1 i put %string1%+++
and string 2 i put %string2%
and concatinated that.
And i have string1 and string2 definitely.
Thank you

A one more approach for concatenating many no. of string variable is writing java service which accepts an document as input. You should create a document in the flow service and create the input variables required to concatenate. The java service will read the variables from the document and then concatenate it.

I created the custome service which also trims and uses a separator also. It is not dependent on fix no. of inputs.

Please find the code snippet below for the service.

IDataCursor idcPipeline = null; // cursor for accessing service pipeline
IDataCursor idcConcatDoc = null; // cursor for accessing service pipeline
//-----------------------------------------------------------------------

IData concatData = null;
String separator = null;
String trimRequired = null;
String keyName = null;
String value = “”;
String output = “”;
int size = 0;
int count = 1;

boolean sizeFlag = false;

try {
// reading input and initialization begins
idcPipeline = pipeline.getCursor(); // accessing pipeline

if (idcPipeline.first("concatData")) {
	concatData = (IData) idcPipeline.getValue();  // retriving actual document from pipeline
} else {
	output = "";
	idcPipeline.insertAfter("value", output);
	idcPipeline.destroy();
	return;
}
if (idcPipeline.first("separator")) {
	separator = (String) idcPipeline.getValue();  		
}

if (separator != null) {
		separator = separator.trim();
} else {
	separator = "";
}
if (idcPipeline.first("trimRequired")) {
	trimRequired = (String) idcPipeline.getValue();  				
}

if (trimRequired != null) {
	trimRequired = trimRequired.trim().toUpperCase();
} else {
	trimRequired = "FALSE";
}
idcPipeline.destroy();	
idcConcatDoc = concatData.getCursor();	// cursor to concat document	

// Main Logic
while (idcConcatDoc.next()) {
	
	if (!sizeFlag) {		// Issue with size method is that, it puts cursor at 
					// last and also if we use idcConcatDoc.first() and then idcConcatDoc.next()
					// cursor will skip to position 2. Hence we are maintaining a flag for this.
		size = IDataUtil.size(idcConcatDoc);	
		idcConcatDoc.first();
		sizeFlag = true;
	}

	value = IDataUtil.getString(idcConcatDoc);	     // get the value for the current key as String	
	if (value == null)
		value = "";		

	if (trimRequired.equals("TRUE") && value != null) {
		value = value.trim();
	} 
	if ((separator != null || separator != "")){
		if (count < size) {
			
			value = value + separator;
		}
	}		
	output = output + value;
	value = null;	
	count++;
}
idcPipeline.insertAfter("value", output);

} catch(Exception e) { // main try catch

	idcPipeline.destroy();		
	throw new ServiceException("Error in concatAll service " + e.toString());
	
}  // end catch

//-------------------------------------------------------------------------