I'm reneging on a function to remove [] from a string. I need to remove the brackets from a string, but the problem I'm having is that those [] can be anywhere in the string.
The code I am using is the following:
function quitarCorchetes(descripcion: string): String;
Begin
if descripcion <> '' then
if descripcion[1] = '[' then
descripcion := Copy(descripcion,2,Length(descripcion)-2);
Result := descripcion;
End;
This code works for strings that are "[algo adentro]"
, the problem is when the string is of type "[algo] mas descripcion"
or "descripcion [algo]".
I don't know if there is any way to do it using a regular expression or something like that. I am very new to pascal and don't have much idea.
You go through the string through a cycle and for each position of the string you ask if the character '[' or the character ']' is found, if any of those characters are found, they are deleted from the string through the Delete procedure.
Delete deletes a substring within a string and takes 3 parameters, the first parameter is the string, the second is the starting position, and the third parameter is the number of characters to delete.
Ex:
Here from the string starting at position 2, 2 characters are going to be deleted, therefore the result would be abefghi, that is, the characters c and d were deleted from the original string
You can use the function
Pos()
to determine the position of a character within a string and, if found within it, remove it, for example this function will remove all occurrences of a character within a string:You could, based on this, write another routine to remove the brackets:
Finally, if you work with a modern version of Pascal (Delphi, FreePascal), you can use the function
StringReplace
, which is found in the unitSysUtils
: