尝试从字符串中删除某个字符,直到字符串只有x个字符

时间:2015-04-04 20:56:41

标签: string delphi

我试图从字符串中删除某个字符,直到该字符串只有x个字符

所以我想要实现的是

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

成为

c:\folder\new\

一次删除一个字符,直到该字符串为3 x' \

{或者我需要多少反斜杠,因为它每次都不一样。}

到目前为止,我已经尝试过:

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(&#39; \&#39;,s)&lt; 4)

编辑..............

感谢所有回复

我永远不知道它需要多少反斜杠....我有一个源文件夹,它一直在变化

然后我有一个目标文件夹,它也会一直在变化 源文件夹(E.G c:\ test)和目标文件夹(E.G D:\ Search Results)

扫描源文件夹以查找文件类型..找到匹配类型后(E.G c:\ test \ somefolder \ anotherfolder.exe) 我用目的地替换了源 (E.G D:\ Search Results \ somefolder \ anotherfolder.exe)。

然后我使用一个函数来计算源文件夹的反斜杠和 搜索结果反斜杠所以

D:\搜索结果\ =两个反斜杠(2) 和 D:\搜索结果\ somefolder \ anotherfolder.exe =四个反斜杠(4)

如果我们采取4-2 + 1 = 3

这是我被困的地方......

我现在知道复制整个源目录需要多少反斜杠。

所以我需要3个反斜杠

如果我们向后计算,我们会

D:\ Search Results \ somefolder \ anotherfolder.exe D:\搜索结果\ somefolder \ anotherfolder(这是错误的,这就是为什么我添加了until (pos ('\',s) = I) and (s[length(s)]='\');

最好是向前计数,以便我们得到

D:\ Search Results \ somefolder \ anotherfolder.exe D:\搜索结果\ somefolder \

再次感谢所有回复。我没有尝试任何建议,但我将调查所有这些,看看我是否可以做一些工作。

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:2)

重复使用PosEx次数,以便在结果中出现反斜杠。当您找到 nth 后,使用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;