When we work with an attribute table in gvSIG, we sometimes want to split the strings in one of the fields, in order to have a substring. For that, we would use the subString operator at the Field calculator, that will return a part of the original string.
For example, if we have an only field with the code of a municipality (two numbers), then a space, and finally the name of the municipality, we would be able to be interested in having a new field with the code, and another one with the name. The subString operator will allow to do it.
The subString command has the following structure:
subString(Parameter 1,Parameter 2, Parameter 3)
where:
- Parameter 1: It is the field name (between square brackets in gvSIG).
- Parameter 2: It is the position of the first character to be split.
- Parameter 3: It is the position of the last character of the substring to be split.
And we have to take into account that the first position will be “0”.
In addition, the Length operator can be used when we want to split the last characters and the length of the different strings in the field is different.
We can have several possible situations, and these options are the most common ones:
- We want to take the initial part of the string, removing the last X characters:
subString([Field],0,length([Field])-X)
(where “Field” will be the field name, and X will be the number of characters of the end to be removed)
Example: Removing the last 3 characters: subString([Field],0,length([Field])-3)
- We want to remove the first X characters, and take the rest of the string:
subString([Field],X,length([Field]))
(where “Field” will be the field name, and X will be the number of characters to be removed at the beginning of the string)
Example: Removing the first 3 characters: subString([Field],3,length([Field]))
- We want to take only the first X characters:
subString([Field],0,X-1)
(where “Field” will be the field name, y “X-1” and “X-1” will be the numbers of characters to be taken)
Example: Taking the first 3 characters: subString([Field],0,2)
- We want to take only the last X characters:
subString([Field],length([Field])-X,length([Field]))
(where “Field” will be the field name, and X will be the number of characters that we want to take at the end of the string)
Example: Taking only the last 3 characters: subString([Field],length([Field])-3,length([Field]))
Here you have a video about it:
We hope that this functionality is useful for you!