Mega Code Archive

 
Categories / Delphi / Strings
 

Work with strings

Title: work with strings Question: This article is string manipulation,and a few example how to create own scripting... Answer: (* In this example I'll try to describe how to manipulate with strings(EASY). For example, we have "Memo1" and "Button1", and we need to find if there is "ShowMessage" word and parse what is between "(" brackets ")". 1.Create Memo (probably named "Memo1") 2.Create Button (named "Button1"), and set caption as "Run". *) // it means-how is our word for showmessage const ShowMessIdent = 'showmessage'; procedure TForm1.Button1Click(Sender: TObject); Var I, MC, P1, P2: Integer; A, S: String; begin {This integer stores number of lines in the memo1} MC := Memo1.Lines.Count; {This variable stores a line number where we start finding...} I := 0; {Main loop} While I {first, we translate each line in the memo to lowercase} S := LowerCase(Memo1.Lines.Strings[I]); {if there is our word, we try to parse what is between brackets..} if pos(ShowMessIdent, S) 0 then begin {it's good to test if there is the first brackets...just in case...} if pos('(', S) 0 then begin // in P1 is position of the first bracket "(" P1 := Pos('(', S); // in P2 is position of the second bracket ")" P2 := Pos(')', S); // once again-it's good to test if is there the second bracket... if P2 // "A" variable stores string between two positions P1 and P2 // length is P2-P1-1 ( A := Copy(S, P1+1, P2-P1-1); // Finally, show text we found.... ShowMessage(A); end else begin ShowMessage('"ShowMessage" found, but hey ! Where are brackets ? :-D ');Break; end; end; // NEVER !!! Don't forget to add this command... :-)) Inc(I, 1); end; end; (* If you are interested in string manipulation I can write more examples "How to "Strings"...something like "Advanced String manipulation" .... because I really bored, the weather is bad...:-)) Send me e-mail if you would need EXE file of this example.... *)