Forum > Beginners
String probelm(Solved)
JLWest:
I have strings and I need to search the strings and return two items from the search.
Search for: (|.| = |; |:|[|,|:= |-) Search items delimited by bar and I can't use Char because some of the items are 2 char.
The string will look like:
'procedure Erase(var f: file );'
'protected procedure TLazLoggerLogGroupList.Remove(const AConfigName: string );'
Need to return the item found '(' and the position in the string;
In the first string it would be '(' and 16.
In the second example 5 items match the criteria but what I'm looking for is '(' and 49.
The probelm is its not always a '(' as the first item in the string. It could be any of the listed items.( (|.| = |; |:|[|,|:= |-) )
I can't come up with a solution.
Thanks
speter:
I'm not totally sure that I understand you search items, but how about this:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);const fields : array of string = ('(', ' = ', '; ', ':', '[', ',', ':= ', '-');var s,u : string; a, i : integer; found : boolean;begin i := 0; for s in memo_input.lines do begin found := false; inc(i); for u in fields do begin a := pos(u,s); if a > 0 then begin memo_msgs.append(format('%d: found "%s" at pos %d',[i,u,a])); found := true; break; end; end; if not found then memo_msgs.append(format('%d: no matches',[i])); end; memo_msgs.append('finished');end;
cheers
S.
JLWest:
@speter
I'll try this out.
What I have doesn't work well, and it pretty ugly.
Thanks for the reply.
Yea, I see how this works. Good solution I think. I'll have to modify it a little and test it out on about 3,100+ strings;
Thanks
Zvoni:
Look at the StrSpn/StrCSpn-API available in libc, or on Windows it‘s part of the OS
Zvoni:
From a POV of a possible algorithm:
You want to find first occurrence of any of those special characters in your string.
1) run through your characters, and check each for first occurrence
2) keep the results somewhere in a key/value pair (Stringlist?), with following specialty: the returned position is the key, your character the value
3) when all characters are checked, sort the Stringlist!
Beware: it‘s a string-sort, meaning you might have to pad out the keys with leading zeros
4) the first key/value pair with key>0 is what you’re looking for
Might have some spare time on monday to write a prove of concept
Edit: or better: instead of using a key/value storage just check position.
characters with position 0 get discarded immediately, the first non zero keep in a „minimum“ variable. Any following positions if greater than minimum get discarded, if lesser than minimum, it becomes the new minimum
Navigation
[0] Message Index
[#] Next page