How to replace the first occurenace of .

Hi All,

Can you please give me the flow code to replace first occurance of the character(dot).

Cheers,
Sasanka

You could do this in a couple ways in Flow service but again that will need to invoke a few java services instead you can use one Java method called String.replace() in a java service to replace the first occurence.

find the first occurance using pub.string.indexOf service,then use pub.string.replace to replace the string

I believe pub.String.replace service replaces all the occurrences of the searchString in v6.5, not sure in later versions.

Ok , If it has to be using flow services then you need to break the string with the first occurence of the DOT as the breaking point at the same time with the DOT included in the first part and use replace service on the first part only and then concat them. You will have only the first occurrence replaced.

A pseudo code of the above logic, you can built this is in wM Flow itself.

 
inputString = "ab.cd.ef.gh";
Str1 = inputString.substring(0, inputString.indexOf(“.”)+1);
Str2 = inputString.substring(inputString.indexOf(“.”)+2, inputString.length);
Str1 = Str1.replace(“.”, “x”);
outString = Str1 + Str2;

Sure there would be other ways also to do this.

-HTH

Use pub.string:replace and a regex to select the first occurring ‘.’ character. I’m not sure what the regex would be.

reamon right, use built-in replace with regex.
1st string identified by ^,
last string identified by $,
special character, such as . and +, should appended with prefix \

so to replace first ., search by ^. with regex true

but, if the .(dot) not in the first char of string, you cant use the built-in replace,
use java service with method of replaceFirst.

String s = “testt…a”;
s = s!=null?s.replaceFirst(“\.”, “[newChar]”):null;

Not quite accurate.

^ is the beginning of the string, not the first occurrence.
$ is the end of the string, not the last occurrence.

^. will work only if . is the first character (as you mention).

Here are the inputs that will do the work with pub.string:replace

inString: the.quick.brown.fox
searchString: .(.*)
replaceString: #$1
useRegex: true

The output will be the#quick.brown.fox

I’m not sure how efficient that will be for a lengthy string but it does the job.

Of course you create your own Java service as suggested, but at least there is an option to leverage what already exists.

about the ^ and the $, its my bad of sentence. but i mean the same.
and thank for correcting the built-in replace string service