Assuming your array is zero-based, you can actually just do array i string i+1 in your loop body and then correct the for to go. Errors No length checking is performed on Content. The space and CR/LF characters are always considered whitespace. The words are separated by Separators and any characters in whitespace are stripped from the strings. Strings use a 1-based index in Turbo Pascal. ExtractStrings splits Content (a null-terminated string) into words, and adds the words to the Strings stringlist. So, for instance, if we had the string "ABCD", we want the array tags in this forum? You have CSS for it and everything, but it gets stripped out when I click Post. Your solution is mostly correct, except that your destination array is 0-based and the 3rd parameter to Copy is the number of characters to copy, not the final index. The problem, as a few repliers seem to have missed, is splitting a string into an array of characters. SplitString returns an array of strings of type that contains the split parts of the original string. Delimiters is a string containing the characters defined as delimiters. My bad.Everyone - I have an actual solution to this problem. SplitString splits a string into different parts delimited by the specified delimiter characters. Got="%s", Expected="%s"',, testResult]) Įdit: code for the CopyRange() function was missing, added now. TestResult := TStringList(TestStrings.Objects) Here is the rough test code I used to ensure it's solid: procedure AddTest(const ATestLine: string const AExpectedResult: array of string) įor i := 0 to Length(AExpectedResult) - 1 do Similarly, just having a delimiter by itself as the input string would result in two blank strings in the TStrings. Note: as per C#'s string.Split(), a blank input string will result in a single blank string in the TStrings. Result := Copy(s, AIndexFrom, AIndexTo - AIndexFrom + 1) Here is the procedure: procedure SplitString(const ASource: string const ADelimiter: Char AValues: TStrings) ĪValues.Add(CopyRange(ASource, 1, i - 1))ĪValues.Add(CopyRange(ASource, lastDelimPos + 1, i - 1)) ĪValues.Add(CopyRange(ASource, lastDelimPos + 1, MaxInt)) įunction CopyRange(const s: string const AIndexFrom, AIndexTo: Integer): string Note: it puts the values into a TStrings, not into an array as the op requested, but can easily be modified to adapt to arrays. I wrote a simple parser that only works for single character delimiters. Works perfectly if you are not expecting lines in your source string. However, I have since found that that solution has a bug whereby it still splits on #13#10 regardless of delimiter. I initially praised the answer from as I needed something that works for Delphi 6 and it appeared to work. System.Delete(Result, Length(Result), 1) įunction Explode(S: String Delimiter: Char): Strings overload įunction Explode(S: String Delimiter: Char Index: Integer): String overload įunction Implode(S: Strings Delimiter: Char): String Implode is very low speed function, but i write it easy. I also incorporated the memory allocation change I suggested, with refinement (I mistakenly suggested the input string might at most contain 50% separators, but it could conceivably of course consist of 100% separator strings, yielding an array of empty elements!)Įxplode is very high speed function, source alhoritm get from TStrings component.Įxplode 134217733 bytes of data, i get 19173962 elements, time of work: 2984 ms. I can't use arrays and the variable that 'calls' the procedure must be a. So if i enter: This is a valid word: 12ab The program must return me: This is a valid word: 12ab Without the spaces, one below the other. It is the trailing separator and notional "empty final element" that I consider the bug fix. I have to create a program that splits phrases (with special characters such as and ) into words using a procedure in pascal. SplitString(':', 'a:b:c:', 2) returns: result = a Similar to the Explode() function offered by Mef, but with a couple of differences (one of which I consider a bug fix): typeįunction SplitString(const aSeparator, aString: String aMax: Integer = 0): TArrayOfString
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |