You need to sign in to do that
Don't have an account?

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
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
You can refer below code:
All Answers
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
You can refer below code:
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
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);