也许很简单,也许很难。(50分)

  • 主题发起人 主题发起人 leonstart
  • 开始时间 开始时间
L

leonstart

Unregistered / Unconfirmed
GUEST, unregistred user!
问题很简单:如何从一个字符串中截取给定的字符子串。
用DELETE不行,因为字符串的长度是不确定的。
 
不见得吧。用过Delphi的查找么?和你说的一样吧。
 
StrSearch
StrSearch returns the index of the first character in a specified sub-string that occurs in a given string. The search starts at the supplied index and is case-sensitive.

function StrSearch(const Substr, Str: string
const Index: Integer = 1): Integer;

Parameters
Substr
The sub-string to search for.
Str
The string in which to search.
Index
The index in Str at which to start the search.
Return Values
One-based index of the first character of SubStr in Str or 0 if SubStr does not occur in the supplied string.



function StrSearch(const Substr, S: AnsiString
const Index: Integer): Integer
assembler;
asm
// make sure that strings are not null

TEST EAX, EAX
JZ @@SubstrIsNull

TEST EDX, EDX
JZ @@StrIsNull

// limit index to satisfy 1 <= index, and dec it

DEC ECX
JL @@IndexIsSmall

// ebp will hold # of chars in Substr to compare, esi pointer to Str,
// edi pointer to Substr, ebx primary search char

PUSH EBX
PUSH ESI
PUSH EDI
PUSH EBP

// set the AnsiString pointers

MOV ESI, EDX
MOV EDI, EAX

// save the (Index - 1) in edx

MOV EDX, ECX

// save the address of Str to compute the result

PUSH ESI

// temporary get the length of Substr and Str

MOV EBX, [EDI-AnsiStrRecSize].TAnsiStrRec.Length
MOV ECX, [ESI-AnsiStrRecSize].TAnsiStrRec.Length

// dec the length of Substr because the first char is brought out of it

DEC EBX
JS @@NotFound

// # of positions in Str to look at = Length(Str) - Length(Substr) - Index - 2

SUB ECX, EBX
JLE @@NotFound

SUB ECX, EDX
JLE @@NotFound

// point Str to Index'th char

ADD ESI, EDX

// # of chars in Substr to compare

MOV EBP, EBX

// clear EAX &amp
ECX (working regs)

XOR EAX, EAX
XOR EBX, EBX

// bring the first char out of the Substr, and
// point Substr to the next char

MOV BL, [EDI]
INC EDI

// jump into the loop

JMP @@Find

@@FindNext:

// update the loop counter and check the end of AnsiString.
// if we reached the end, Substr was not found.

DEC ECX
JL @@NotFound

@@Find:

// get current char from the AnsiString, and /point Str to the next one.
MOV AL, [ESI]
INC ESI

// does current char match primary search char? if not, go back to the main loop

CMP AL, BL
JNE @@FindNext

// otherwise compare SubStr

@@Compare:

// move # of char to compare into edx, edx will be our compare loop counter.

MOV EDX, EBP

@@CompareNext:

// check if we reached the end of Substr. If yes we found it.

DEC EDX
JL @@Found

// get last chars from Str and SubStr and compare them,
// if they don't match go back to out main loop.

MOV AL, [EDI+EDX]
CMP AL, [ESI+EDX]
JNE @@FindNext

// if they matched, continue comparing

JMP @@CompareNext

@@Found:
// we found it, calculate the result and exit.

MOV EAX, ESI
POP ESI
SUB EAX, ESI

POP EBP
POP EDI
POP ESI
POP EBX
RET

@@NotFound:
// not found it, clear result and exit.

XOR EAX, EAX
POP ESI
POP EBP
POP EDI
POP ESI
POP EBX
RET

@@IndexIsSmall:
@@StrIsNull:
// clear result and exit.

XOR EAX, EAX

@@SubstrIsNull:
@@Exit:
end;
 
以前好象有这方面的问题,要么自己写一个函数。
 
用length取字符串长度,用pos定位,用delete删除不行吗?
 
对,DELPHI可以完成
 
多人接受答案了。
 
后退
顶部