# String Function

**URL:** https://forum.cloverdx.com/t/string-function/1607
**Category:** CloverDX Platform
**Created:** [March 27, 2015, 12:00am UTC](https://forum.cloverdx.com/t/string-function/1607 "2015-03-27T00:00:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![yichuansancun](https://avatars.discourse-cdn.com/v4/letter/y/8e7dd6/32.png) [@yichuansancun](https://forum.cloverdx.com/u/yichuansancun)
#### Post date: [March 27, 2015, 12:00am UTC](https://forum.cloverdx.com/t/string-function/1607/1 "2015-03-27T00:00:00Z")

</div>

I have the following strings:

PRJ\_D9\_WEST\_LE\_CLIENT\_B  
PRJ\_D9\_WEST\_LE\_CLIENT\_A  
PRJ\_D9\_WEST\_LE\_CLIENT\_C

How do I separate the string to get the following results:

Region | Segment | Client Name  
WEST | LE | CLIENT\_B  
WEST | LE | CLIENT\_A  
WEST | LE | CLIENT\_C

I think it is a combination of Substring and indexOf, but cannot wrap my head around. Thanks, Perri

---

<div class="post-metadata">

### Author: ![slechtaj](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@slechtaj](https://forum.cloverdx.com/u/slechtaj)
#### Post date: [March 30, 2015, 3:21pm UTC](https://forum.cloverdx.com/t/string-function/1607/2 "2015-03-30T15:21:49Z")

</div>

Hi Perri,

There are multiple ways how to do this. Basically it depends on the rest of your data. At first you might use the split function which can split the string into an array. So for example this function:

```auto
myArray = split($in.0.yourString, "_");

```

will produce the following list (for the first record):

```auto
["PRJ", "D9", "WEST", "LE", "CLIENT", "B]

```

The fields of the aforementioned array may be mapped to output just like this:

```auto
$out.0.firstOutField = myArray[2];
$out.0.secondOutField = myArray[3];
$out.0.thirdOutField = concat(myArray[4],"_",myArray[5)];

```

As I said the actual solution for your case is fully dependent on all your data. Therefore please bear above example as an idea how you can start designing your transformation. The actual transformation design will require you to know the exact format of the data you can receive on input.

You should keep in mind that the second argument of the split function is regular expression, and thus it may contain advanced generic delimiters.

Hope this helps.

---

<div class="post-metadata">

### Author: ![yichuansancun](https://avatars.discourse-cdn.com/v4/letter/y/8e7dd6/32.png) [@yichuansancun](https://forum.cloverdx.com/u/yichuansancun)
#### Post date: [April 4, 2015, 5:23pm UTC](https://forum.cloverdx.com/t/string-function/1607/3 "2015-04-04T17:23:18Z")

</div>

Thanks, I tried your code but getting the following error:

11: $out.0.Client = iif(myArray[5]==null,myArray[4],myArray[4] + “\_”+ myArray[4]);  
^^^  
----------------------- CTL2 snippet -----------------------  
variable “myArray” (string) :  
[PRJ, D9, INTERNAL, TRAINING]

Not all my strings have 4th or 5th element. In this case, how can I check how many elements in an array?

Perri

---

<div class="post-metadata">

### Author: ![dpavlis](https://avatars.discourse-cdn.com/v4/letter/d/90ced4/32.png) [@dpavlis](https://forum.cloverdx.com/u/dpavlis)
#### Post date: [April 7, 2015, 5:47pm UTC](https://forum.cloverdx.com/t/string-function/1607/4 "2015-04-07T17:47:15Z")

</div>

You may use [length()](http://doc.cloveretl.com/documentation/UserGuide/topic/com.cloveretl.gui.docs/docs/container-functions-ctl2.html#ctl2-length%5F2) function to get the actual length of the array resulting from the split() function.

Example:

```auto
myArray = split($in.0.yourString, "_");
switch(length(myArray)){
case 5:
  //do something
  break;
case 4:
 // do something else
 break;
default:
  // do default handling
}

```
