Within an attribute table, I'm trying to replace one string with another using the replace
function. As an example, there are a number of features whose field "NAME"
contains the string '(B)' and I'm wanting to replace this with the word 'County'. I'm selecting the 'update existing field' checkbox and using the following expression
replace('(B)', '(B)', 'County')
The end result is that the field "NAME"
for every feature is replaced by 'County' irrespective of whether or not the field "NAME"
originally contained the string '(B)'.
-
Please, do not forget about "What should I do when someone answers my question?"Taras– Taras ♦2022年10月26日 16:09:49 +00:00Commented Oct 26, 2022 at 16:09
5 Answers 5
replace( '(B)','(B)','County')
tells QGIS to replace the '(B)' in '(B)' with 'Country'.
You want
replace("Name",'(B)','Country')
= replace the '(B)' in "Name" with 'Country'.
Note that the double quotes indicate a field name while the single quotes refer to a static string. These are not interchangeable.
-
1@GrahamD please accept the answer if it solved your problemunderdark– underdark2017年01月09日 20:53:04 +00:00Commented Jan 9, 2017 at 20:53
The proper syntax is:
replace([field_name], '(B)', 'Country')
Firstly, open the Field Calculator and select the replace function from the string option like the following picture:
Secondly, tells QGIS to replace the replace ("Field Name", '(B)', 'Country')
. For example in this case
replace("MES_TIP", 'T', 'Rock')
Thirdly, please choose the output field type: text(string), and then determine the field name
The interface looks a little different now and you have to select the column from the menu in the drop-down on the left-abc name
I also noted that words in the single quotations have to be exact including no extra spaces. (which stumped me briefly)
I was replacing the word Road with Rd. in this example.
replace("name", 'Road', 'Rd.')
Press Update Filtered to make the change. It can take a while...as this OSM file did.
Filtering out a huge table would help speed up the replacement process.
There is also a small plugin available now, which wraps regex_replace in a small GUI:
-
It's painfully slow... not usable with larger datasets, and does not have a cancel button to stop action. Quite bad "plugin"sys49152– sys491522023年04月19日 08:04:55 +00:00Commented Apr 19, 2023 at 8:04
Explore related questions
See similar questions with these tags.