4

How to use the field calculator to replace the character - in part of the values with the character "B" at the end of this value? I also want to replace the other values without symbol - replace with the symbol "F" at the end of these values. Like this -0345 to 0345B, 0341 to 0341F.

enter image description here

2
  • What is type of linecode, integer or string(text)? – Kadir Şahbaz Dec 22 '20 at 12:37
  • It's string type – Ivan M Dec 22 '20 at 12:43
4

You can use regexp_replace function in field calculator:

enter image description here

The expression

regexp_replace(  "linecode", '^-(.*)$', '\\1B')

will replace '-' + 'any string' with 'any string' + 'B'. So it will change string '-0345' to '0345B'. \\1 refer to the expression part in brackets. You may give more specific regexp instead of '.*'.

enter image description here

The second expression will add 'F' to numeric strings:

regexp_replace( "linecode", '^([0.9][0-9]*)$', '\\1F')

It will replace only string made of numeric characters [0.9], so you can use it after the previous expression without selection in the table.

2
  • My answer is easy to understand, but this is more professional. :) – Kadir Şahbaz Dec 22 '20 at 13:00
  • Thanks guys. Both methods works – Ivan M Dec 22 '20 at 13:28
4

Use this expression: if(left(linecode, 1) = '-', substr(linecode, 2) + 'B', linecode + 'F')

I suggest to create new field instead of changing the existing one. You may need the original one later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.