Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

string - Delete text after specific character

I have the text inside TEdit box: '955-986, total = 32'

How would I delete all text after the comma, so it will only left '955-986'

I tried to limit the TEdit Length, but it's not working as I wanted it to be.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What if there'd be no comma? full non-cut string or empty string ?

Below is your idea of limiting string length, but only applied if at least one comma was found.

var 
    tmpStr:string;
    commaPosition:integer; 
begin
  tmpStr := Edit1.Text;
  commaPosition := pos(',',tmpStr);
  if commaPosition > 0 then begin
     SetLength(tmpStr, commaPosition - 1);
     Edit1.Text := tmpStr;
  end;
end;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...