This content has been marked as final. Show 3 replies
> I need to replace commas in a string with , but only between " ".
Do you mean you wish to replace " , " with ,, where there is a space before and after the comma? Then this should do you
<cfset modifiedStr = Replace(str," , ","&##38;##44;", "all")>
This is the sort of thing I am trying to do. A simple replace would be great but its not comprehensive enough.
<cfset fileContentsArray = 'This is a long line of text that contains several commas, but only the ones surrounded by "speech marks, will be changed"'>
<cfset reg = '"([*a-z][*0-9])(,)([*a-z][*0-9])"'>
<cfset fileContentsArray = reReplace(fileContentsArray, reg , "#chr(44)#" , "ALL")>
I need to replace all commas between all of the " " with chr(44) or ,
I'm importing a CSV file and one of the "fields" is a description that more than likely will contain commas. I have set it so that the description field is surrounded by " " so that (in theory) I can isolate the commas in the description that would otherwise break the documents format.
Thanks for the reply
I would do it in a Java class. Then use Coldfusion to create an object from that, for any given string. Equip the object with the following functionality.
Function 1: For any given string s, create the corresponding String object, new String(s). Use a loop and the function indexOf("\"", fromIndex) to iteratively pick out the indices of all the quotes. If a quote occurs at index N, the next iteration starts with the value fromIndex=N+1. Store the quotes' indices in pairs (opening and closing quotes), for example, in a 2D array.
Function 2: Given the original string object and an array of (beginIndex, endIndex) integer pairs, use the function charAt(n) to find where a comma occurs in the quoted parts of the string, and replace each such occurrence with , . Here n is a dummy integer variable that ranges from beginIndex-1 to endIndex-1 for each pair of indices.
The following snippet is an attempt I made with Coldfusion. However, it mistakenly fails to see indexOf as a function of the String object. And I, alas, am too busy with other things to pursue this line of interrogation with Coldfusion at the moment. I hope the snippet conveys enough of the flavour of Function 1 to inspire you to a solution.