2015-04-04 97 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