请教有关字符串处理的问题(30分)

H

hello2

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大侠:
请问如何从一个字符串中读出特定位置的字符,并转化为整数,
进行相应的处理后再写入原位置。(用到哪些函数)
致谢
 
我以前有一些小函数不知对你是否有用:
//读一字符串中某一位置字符值
function GetCharCode(pStr:pChar;Index:Integer):BYTE;
begin
if Index>StrLen(pStr)-1 then
raise ERangeError.Create('The Index is Out of Range');
Inc(pStr,Index);
result:=BYTE(pStr^);
end;
//替换一字符串某一位置字符值
procedure ReplaceChar(pStr:pChar;Index:Integer;CharCode:BYTE);
begin
if Index>StrLen(pStr)-1 then
raise ERangeError.Create('The Index is Out of Range');
Inc(str,Index);
BYTE(str^):=CharCode;
end;


 
对不起打错了,应为

//读一字符串中某一位置字符值
function GetCharCode(pStr:pChar;Index:Integer):BYTE;
begin
if Index>StrLen(pStr)-1 then
raise ERangeError.Create('The Index is Out of Range');
Inc(pStr,Index);
result:=BYTE(pStr^);
end;
//替换一字符串某一位置字符值
procedure ReplaceChar(pStr:pChar;Index:Integer;CharCode:BYTE);
begin
if Index>StrLen(pStr)-1 then
raise ERangeError.Create('The Index is Out of Range');
Inc(pStr,Index);
BYTE(pStr^):=CharCode;
end;

 
常用的有:
1. COPY(STR,STARTINDEX, COUNT)
例如 :
STR := 'ABCDEFG'
SUBSTRING := Copy(STR,2, 4) // 值 为: BCDE ;

2. STR := '1234565'
SUBSTRING := COPY(STR,4,2) //'45'
N := STRTOINT(SUBSTRING);
3 写回原处 DELETE(SUBSTRING,4,2;
INSERT(STR,MODIFIED_STR,4);




 
给你解决问题的全部程序。(其实你只要有点耐心,也能写出来^-^)
=======================================================
function getNum(s : String):string;
//该函数有待改进,可取浮点数及其它
var i,j : integer;
begin
result := '';
for i := 1 to length(s) do
if s in ['0'..'9'] then
begin
for j := i to length(s) do
if not(s[j] in ['0'..'9']) then break;
result := copy(s,i,j-i);
break;
end;
end;

function calcNum(s : String):string;
var inValue,outValue : integer;
//这里是你的动算处理程序
begin
inValue := StrToInt(s);
outValue := inValue * 10;
result := IntToStr(outValue);
end;

//以下是调用主程序
var testS,tempS : String;
curNum: string;
begin
testS := 'aiming1234aimt123'; //初值

tempS := testS; testS := '';
curNum := getNum(tempS);
while curNum <> '' do
begin
testS := testS + copy(tempS,1,pos(curNum,tempS)-1) + calcNum(curNum);
tempS := copy(tempS,pos(curNum,tempS) + length(curNum),Length(tempS));
curNum := getNum(tempS);
end;
writeln(testS); //这里的testS就是已经替换完成的串了
end.
 
无论是String还是PChar字符串都可以当作数组使用,要想得到某一个字符,直接
用下标即可,唯一需注意的是String下标从1开始,PChar下标从0开始。
对其进行计算只需用Chr 和 Ord 即可。

var
S: String;
begin
S := 'ABCDEFG';
WriteLn(Ord(S[3]); // 67
S[3] := Chr(Ord(S[3]) + 3);
WriteLn(S[3]); // 'F'
end.
 
字符串:Pos,copy,val主要的!
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
954
SUNSTONE的Delphi笔记
S
S
回复
0
查看
775
SUNSTONE的Delphi笔记
S
D
回复
0
查看
828
DelphiTeacher的专栏
D
D
回复
0
查看
630
DelphiTeacher的专栏
D
顶部