function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
meghna nmeghna n 

remove multiple commas between two strings

Hello
I am having two strings separated by multiple commas. 
I need to retain only a single comma between the two strings.

for example I have as follows var cptCode = G1077,,,,,,,,,,D3456 

so after removing multiple commas, I will have like this :

var cptCode = G1077,D3456 

The user can enter 2 commas or 5 commas between the two strings.
Maximum number of commas between the two strings will be 10.


 I request the members to let me know how to achieve this.

Thanks
Meghna
Best Answer chosen by meghna n
Danish HodaDanish Hoda
Hi Meghna,
You can refer below code:
let str = "G0177,90792,,,,,";
let strArray = [];
strArray = str.split(",");
var strVar = "";

for(let i=0; i<strArray.length; i++){
  if(strArray[i]){
       strVar+= strArray[i]+",";
  }
    if(i<(strArray.length-2)){
      if(strArray[i+1]){
         strVar += strArray[i+1]+",";
      }
      i++;
   }
}
var lastIndex = strVar.lastIndexOf(",");
strVar  = strVar.substring(0,lastIndex)
console.log(" strVar "+strVar);

 

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Meghna,

To my knowledge you should be able to use a custom validation rule to check if the entered text is in proper format.

While implementing a validation rul please do make a note of the considerations listed in the below link:

>> https://help.salesforce.com/articleView?id=fields_validation_considerations.htm&type=5

In case if this was helpful in your scenario can you please close the thread and mark this as best answer so that it can be useful for others in the future and also helps in heeping the community clean.

Regards,
Anutej
Danish HodaDanish Hoda
Hi Meghna,
You can refer below code:
let str = "G0177,90792,,,,,";
let strArray = [];
strArray = str.split(",");
var strVar = "";

for(let i=0; i<strArray.length; i++){
  if(strArray[i]){
       strVar+= strArray[i]+",";
  }
    if(i<(strArray.length-2)){
      if(strArray[i+1]){
         strVar += strArray[i+1]+",";
      }
      i++;
   }
}
var lastIndex = strVar.lastIndexOf(",");
strVar  = strVar.substring(0,lastIndex)
console.log(" strVar "+strVar);

 
This was selected as the best answer
sachinarorasfsachinarorasf
Hi Meghna,

Please use the below code to get reqiured solution:
 var str = "G1077,,,,,,,,,,D3456";
        var h=0,k=0,r=0;
        for(let i=0; i<str.length; i++){
            var res = str.charAt(i);
            console.log(res);
            if(res == "," && h==0){
                k=i;
                h=k;
                console.log('h::'+h);
            }else if(res == ","){
                r = i;
                console.log('r::'+r)
            }
        }
        var str2 = "G1077,,,,,,,,,,D3456";
        var res1 = str.slice(0,h);
        var res2 = str2.slice(r,str2.length+1);
        var result = res1+res2;
        console.log('result:::'+result);
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com
meghna nmeghna n
Thanks to all of you ...
Ramesh Goud ThummalaRamesh Goud Thummala
Hi, 
Plese find the below logic if needed,I have reduced the complexity of logic to achive this.

let str = "G0177,,,,90792,,,,,";
let strArray = [];
strArray = str.split(",");
var strVar = '';

for(let i=0;i<strArray.length;i++){
      if(strArray[i]!= ''){
   
          if(strVar !=''){
              strVar+=strArray[i];
          }
          else{
              strVar += strArray[i]+",";
              }
     }
  }
console.log(strVar);