To parse a string in Delphi, you can use one of the string manipulation functions provided by the language. Here are a few examples of how you can parse a string:
Extracting a substring: You can use the Copy function to extract a specific portion of a string. For example:
1 2 3 4 5 6 7 8 |
var FullString: string; SubString: string; begin FullString := ‘This is a test string’; SubString := Copy(FullString, 1, 4); // SubString will be ‘This’ SubString := Copy(FullString, 5, 7); // SubString will be ‘is a t’ end; |
Splitting a string into an array of substrings: You can use the SplitString function to split a string into an array of substrings based on a specified delimiter. For example:
1 2 3 4 5 6 7 8 |
var FullString: string; SubStrings: TStringDynArray; begin FullString := ‘This,is,a,test,string’; SubStrings := SplitString(FullString, ‘,’); // SubStrings will be [‘This’, ‘is’, ‘a’, ‘test’, ‘string’] end; |
Parsing a string with a specific format: If you have a string with a specific format, you can use the Scanf function to parse it and extract the individual pieces of data. For example:
1 2 3 4 5 6 7 8 9 10 |
var FullString: string; Name: string; Age: integer; Gender: char; begin FullString := ‘John,25,M’; Scanf(FullString, ‘%s,%d,%c’, [@Name, @Age, @Gender]); // Name will be ‘John’, Age will be 25, and Gender will be ‘M’ end; |
These are just a few examples of how you can parse a string in Delphi. There are many other string manipulation functions available in the language, so you may want to refer to the Delphi documentation for a complete list of options.
Leave a Reply