• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Replacing characters between specific characters

Guest
Jul 12, 2006 Jul 12, 2006

Copy link to clipboard

Copied

Hello,

I need to replace commas in a string with , but only between " ".

Does anyone know how to do this. I have looked high and low and tried many things, like trying to find where the first " starts and the last " but not being able to loop through the rest of the string to find other occurences.

I would be extremly grateful for someones help/advice

Chris
TOPICS
Advanced techniques

Views

452

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 12, 2006 Jul 12, 2006

Copy link to clipboard

Copied

> 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")>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 12, 2006 Jul 12, 2006

Copy link to clipboard

Copied

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")>

<cfoutput>
#fileContentsArray#
</cfoutput>

I need to replace all commas between all of the " " with chr(44) or &#44;

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

Chris

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 14, 2006 Jul 14, 2006

Copy link to clipboard

Copied

LATEST
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 &#44; . 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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation