cancel
Showing results for 
Search instead for 
Did you mean: 

Replace last comma with and

dhiraj-267
Member
0 Kudos

I want to replace last comma with 'and' in sap crystal report.

I tried 'replace' and 'instr' in formula but not getting desired output.

Below is the example

example:

Jack, roy, mark, rohit, andrew ....so on

I want to replace only the last comma i.e “rohit” and “andrew” and change it to the word “and”, so the string becomes:

Jack, roy, mark, rohit and andrew

Thanks in advance

Accepted Solutions (0)

Answers (2)

Answers (2)

DellSC
Active Contributor

Here's another way to do it that's not as complicated:

local stringvar myText := "Jack, roy, mark, rohit, andrew";
local NumberVar commaAt := InStrRev(myText, ",");
left(MyText, commaAt-1) + Replace(MyText, ",", " and", commaAt)

InStrRev starts at the end of the string to find the character or string you're looking for.

-Dell

ido_millet
Active Contributor

Dell's solution is indeed nicer 🐵

ido_millet
Active Contributor
0 Kudos

Try something like this:

local stringvar myText := "Jack, roy, mark, rohit, andrew";
local stringvar array myArray := Split(myText, ", ");
local stringvar result := myArray[1];
local numbervar i;
For i := 2 to Ubound(myArray)-1 step 1 do
(
result := result + ", " + myArray[i];
);
result := result + " and " +  myArray[Ubound(myArray)];