2015-04-04 138 views
1

我想從一個字符串中刪除某個字符,直到字符串只有字符x個的x個試圖從一個字符串中刪除某個字符,直到字符串只有字符

所以我會想實現是

c:\folder\new\stuff\all\more\hello\awesome\to.dll 

變得

c:\folder\new\ 

通過一次刪除一個字符,直到字符串posses 3× '\'

{或者我怎麼過很多反斜槓要求它posses因爲它不會是相同的每一次。}

到目前爲止,我曾嘗試:

function RemoveLastPart(var s: string; I : integer): string; 
begin 
    repeat 
    delete(s, length(s),1) 
    until (pos ('\',s) = I) and (s[length(s)]='\'); 
    result := s; 
end; 

function RemoveStr(var s: string; I : integer): string; 
begin 
    while (pos ('\',s) = I) do 
    begin 
    delete(s, length(s),1); 
    result := s; 
end 
end; 

但這是行不通的,因爲你的路徑仍然可能是

c:\folder\new\stuff vs c:\folder\new\ 

我在猜測我錯誤地使用了pos。我想如果一個字符串擁有「\」然後做一些事情。

因爲我永遠不知道路徑將會到達多長時間,或者它可能包含或不包含哪些文件夾,這使得它有點棘手。

我自己也嘗試(pos ('\',s) < I)和1

增加我所以,如果我想我是3我要說(POS( '\',S)< 4)

編輯.... ..........

感謝所有回覆的

我永遠不會知道它有多少個反斜槓需要....我有一個時刻都在變化源文件夾

然後我有一個目標文件夾,它也一直在改變。 (EG c:\ test)和目標文件夾(EG D:\搜索結果)

掃描源文件夾中的文件類型。一旦找到匹配類型(EG c:\ test \ somefolder \ anotherfolder.exe) 我用目的地 (EG D:\ Search Results \ somefolder \ anotherfolder.exe)替換源文件。

我然後使用函數來計算兩個源文件夾反斜線和 搜索結果反斜槓所以

d:\搜索結果\ =兩個反斜槓(2) 和 d:\搜索結果\ somefolder \ anotherfolder.exe =四個反斜槓(4)

,如果我們的取4-2 + 1 = 3

這是我在哪裏卡住了...

我現在知道需要多少個反斜槓才能複製整個源目錄。

,所以我需要3反斜槓

如果再算上向後我們得到

d:\搜索結果\ somefolder \ anotherfolder.exe d:\搜索結果\ somefolder \ anotherfolder(這是錯誤的,這就是爲什麼我添加until (pos ('\',s) = I) and (s[length(s)]='\');

倒不如來算前鋒,所以我們得到

d:\搜索結果\ somefolder \ anotherfolder.exe d:\搜索結果\索姆efolder \

再次感謝所有回覆。我還沒有嘗試任何建議,但我會研究所有這些,看看我能否做出一些工作。

+0

方法Pos總是返回特定字符串內子字符串的第一個ocurnace的位置。所以在你的例子中,Pos總是返回3。因此,我建議您使用PosEx來代替,因爲它允許您指定從哪個位置開始搜索特定的子字符串。 – SilverWarior 2015-04-04 21:16:57

+0

@SilverWarior,Delphi的新版本具有模擬PosEx的Pos重載。 – 2015-04-04 21:20:28

+0

嗨謝謝,即時通訊運行xe7 ..會給這一炮謝謝! @SilverWarior – 2015-04-04 21:24:34

回答

3

既然你是唯一尋找特定的字符,你也可以使用另一種方法,只需迭代字符串中的所有字符並計算你已經找到特定字符的次數。

以下是實現此目的的代碼;

//This function returns desired substring as result and thus does not modiffy input 
//string 
//Use this function when it is required for your InputStr to remain intact for further 
//processing 
function ExtractSubString(InputStr: String; SearchedChar: Char; Count: Integer): String; 
var Pos: Integer; 
    TimesFound: Integer; 
begin 
    //Set initial values to local variables 
    TimesFound := 0; 
    //Set the default result of the method 
    //While this is not needed with string results it is still good practice 
    //Setting default result is required with most other result types so you don't 
    //end up with uninitialized result which can have random value stored in it. 
    Result := ''; 
    //Iterate through all characters in a string 
    //NOTE: On moobile platforms strings are Zero-based so modiffy the code acordingly 
    //to start with Pos 0 
    for Pos := 1 to Length(InputStr) do 
    begin 
    //Check if specific character matches the one we are looking for 
    if InputStr[Pos] = SearchedChar then 
    begin 
     //Increase the counter which stores how many times have we already found 
     //desired character 
     TimesFound := TimesFound+1; 
     //Check if we found desirecd character enough times 
     if TimesFound = Count then 
     begin 
     //Copy desired part of the input string to result 
     Result := Copy(InputStr,0,Pos); 
     //Use break command to break out of the loop prematurely 
     Break; 
     end; 
    end; 
    end; 
end; 


//This procedure modifies existing string instead of making a new shortened string 
//You need to provide existing string variable as InputStr parameter 
//Use only if you need to process string only once othervise use ExtractSubstring 
procedure ShortenString(var InputStr: String; SearchedChar: Char; Count: Integer); 
var Pos: Integer; 
    TimesFound: Integer; 
begin 
    //Set initial values to local variables 
    TimesFound := 0; 
    //Iterate through all characters in a string 
    //NOTE: On moobile platforms strings are Zero-based so modiffy the code acordingly 
    //to start with Pos 0 
    for Pos := 1 to Length(InputStr) do 
    begin 
    //Check if specific character matches the one we are looking for 
    if InputStr[Pos] = SearchedChar then 
    begin 
     //Increase the counter which stores how many times have we already found 
     //desired character 
     TimesFound := TimesFound+1; 
     //Check if we found desirecd character enough times 
     if TimesFound = Count then 
     begin 
     //Adjust the length of the string and thus leaving out all the characters 
     //after desired length 
     SetLength(InputStr,Pos); 
     //Use break command to break out of the loop prematurely 
     Break; 
     end; 
    end; 
    end; 
end; 
+0

嗨,謝謝你SilverWarior。真棒作品像一個魅力。感謝你去了非常詳細的一切,並且很好地解釋了一切。 – 2015-04-04 22:30:51

+0

如果使用基於1的字符串,應該複製(...,0,...) – 2015-04-05 07:33:10

+0

我編輯了我的答案,以提供另一種修改現有字符串的方法,而不是返回所需子字符串的新副本。注:我已經改變了舊方法的名稱,使其更適合它的功能。因此,您需要修改使用它們的代碼,因爲舊函數名稱用於新過程。 – SilverWarior 2015-04-05 09:37:58

2

重複使用PosEx多次,因爲您希望結果中出現反斜槓。當您隨後找到了第n個,使用SetLength()砍串,或Copy()如果您想保留原始的字符串,返回縮短的字符串作爲函數結果,例如:

function ChopAtNthBackslash(const s: string; n: integer):string; 
var 
    i, k: integer; 
begin 
    k := 0; 
    for i := 1 to n do 
    begin 
    k := PosEx('\', s, k+1); 
    if k < 1 then Exit(''); 
    end; 
    Result := Copy(s, 1, k); 
end; 
+0

真棒感謝湯姆布倫伯格正是我所需要的。 – 2015-04-04 22:29:42

相關問題