我有一個包含字母、數字和其他字符的字串。
我想從該字串中洗掉所有數字、點和逗號
之前:' Axis moving to new position - X-Pos: 5.4mm / Y-Pos: 3.5mm'
之后:' Axis moving to new position - X-Pos mm / Y-Pos mm'
不幸的是 string.replace() 只替換一個字符。所以我需要幾行。
如何避免逐行撰寫每個替換?
sString := sString.Replace('0', '');
sString := sString.Replace('1', '');
sString := sString.Replace('2', '');
sString := sString.Replace('3', '');
sString := sString.Replace('3', '');
...
sString := sString.Replace(':', '');
sString := sString.Replace('.', '');
uj5u.com熱心網友回復:
盡管OP 自己的解決方案很好,但它的效率有點低。
為了完整起見,這里有一個稍微優化的版本:
function RemoveCharsFromString(const AString, AChars: string): string;
begin
SetLength(Result, AString.Length);
var ActualLength := 0;
for var i := 1 to AString.Length do
begin
if SomePredicate(AString[i]) then
begin
Inc(ActualLength);
Result[ActualLength] := AString[i];
end;
end;
SetLength(Result, ActualLength);
end;
該演算法獨立于特定謂詞。在這種情況下,謂詞是Pos(AString[i], AChars) = 0。
uj5u.com熱心網友回復:
此解決方案使用非常少的代碼行。
我只是在每次出現應該洗掉的字符時拆分字串。在那之后,我把這些碎片放在一起,沒有洗掉的字符。
uses System.SysUtils;
function RemoveCharsFromString(sFullString: string; sCharsToBeRemoved: string): string;
var
splitted: TArray<String>;
begin
splitted := sFullString.Split(sCharsToBeRemoved.ToCharArray());
Result := string.Join('', splitted);
end;
uj5u.com熱心網友回復:
有多種方法可以解決此問題。這里有三個解決方案。
解決方案 1
您可以簡單地遍歷源字串,檢查每個字符以判斷它是否是需要洗掉的字符之一。
//Simple function that loops through all characters of the source strings removing them one by one
//It is manipulating the same string all the time
function Removechars1(sourceString: string; sCharsToBeRemoved: string):string;
var I: Integer;
begin
//Assign value of the source string to the result so we can work with result strin from now on
result := SourceString;
//Loop throught the whole result sring starting at end searching for characters to be removed
//We start at the end because when we will be removing characters from the string its length
//will be decreasing.
for I := Length(result) downto 1 do
begin
//Use Pos function to see if specific character in the result string can also be found
//in the sCharsToBeRemoved and therefore needs to be removed
if Pos(result[i], sCharsToBeRemoved) <> 0 then
begin
//If so we delete the specific character
Delete(result,I,1);
end;
end;
end;
解決方案 2
第二種解決方案與第一種解決方案類似,但它依賴于在結果中添加不可移除的字符。它比第一個解決方案稍慢
//Slightly faster function that also loops through the whole sourceString character by character
//and adds such characters to result string if they are not present in sCharsToBeRemoved string
function RemoveChars2(sourceString: string; sCharsToBeRemoved: string):string;
var I: Integer;
begin
//Prepare enpty string for our result strung to which we will be copying our end characters
result := '';
//Loop through the whole string
for I := 1 to Length(sourceString) do
begin
//If specific haracter can't be found in sCharsToBeRemoved copy that character to the
//result string
if Pos(sourceString[I], sCharsToBeRemoved) = 0 then
begin
result := result sourceString[I];
end;
end;
end;
解決方案 3
第三種解決方案依賴于字串助手來替換特定字符。這個是迄今為止最快的三個,需要的時間大約是第一個解決方案處理相同作業所需時間的一半
//Another approach of removing characters from source string that relies on Replace string helper
function RemoveChars3(sourceString: string; sCharsToBeRemoved: string):string;
var I: Integer;
begin
//Assign value of the source string to the result so we can work with result strin from now on
result := sourceString;
//Loop through the sCharsToBeRemoved string so we can then call Replace string helper in order
//to replace all occurrences of such character in sourceString;
for I := 1 to Length(sCharsToBeRemoved) do
begin
result := result.Replace(sCharsToBeRemoved[I],'');
end;
end;
這種方法的主要優點是它非常快并且可以很容易地修改為能夠洗掉整個子字串而不僅僅是單個字符。
PS:在我的測驗中,您的解決方案實際上是最慢的,需要比我的第一個解決方案多 20% 的時間
TestTring
jfkldajflkajdflkajlk?fjaskljflakjflkdasjflk?jdfkldafjadklfjadklfjaldkakljfkldajflk?adjslfkjadklfjlkad?jflkajdfl?kjadlkfjladkdjfkladjflkadjflk?jadkl?fjaldkjfkladjfklajflkadjfkadgfkljdklfjawdkojfkladsjfl?aksdjdfkl?asjdkl?fdfkl?jadslkdfjlka
CharsToBeRemoved
asjk
Solution 1
1230 ms
Solution 2
1263 ms
Solution 3
534 ms
Your solution
1574 ms
uj5u.com熱心網友回復:
string.Replace 有一個多載,您可以在其中傳遞標志來替換所有而不是一個。例子:
sString := sString.Replace('1', '', [rfReplaceAll, rfIgnoreCase]);
編輯:等效的字串串列:
sString.Text := sString.Text.Replace('1', '', [rfReplaceAll, rfIgnoreCase]);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/471638.html
上一篇:Delphi-檢查Unicode字符是否出現在一組字符中?
下一篇:更改所有對話框的整個字體樣式
