将字符串反向直到遇到特定字符。

huangapple go评论94阅读模式
英文:

Get the string backward until it hit certain character

问题

抱歉,如果这个问题之前已经被问过的话。但是我找不到它。

实际上有没有一种方法可以从后往前获取并截取字符串,直到遇到特定的字符?

这是目前正在工作的代码。

  1. string path = "LivingOrganism/Animal/Bird/Chicken.exe";
  2. public void SomeFunc()
  3. {
  4. // 从后往前计算字符串,直到遇到分隔符
  5. string[] splitted = path.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries).ToList().ToArray();
  6. string combineStr = "";
  7. for (int i = 0; i < splitted.Count() - 1; i++)
  8. combineStr += splitted[i] += "/";
  9. path = combineStr;
  10. print("results: " + path);
  11. }

所以每次调用SomeFunc()时;
第一次调用是 LivingOrganism/Animal/Bird/
第二次调用是 LivingOrganism/Animal/
第三次调用是 LivingOrganism/
第四次调用是
这正是我想要做的。

然而,是否实际上有一种优化的方法呢?
感觉有点丑陋,我需要先拆分字符,然后在循环中重新组合它们。

英文:

Sorry if this question has been asked before. But I am unable to find it.

Is there actually a way to get and cut the string from backwards until it hit certain char ?

This is currently a working code.

  1. string path = &quot;LivingOrganism/Animal/Bird/Chicken.exe&quot;;
  2. public void SomeFunc()
  3. {
  4. //count the string from the back until it hit the seperator
  5. string[] splitted = path.Split(new string[] { &quot;/&quot; }, StringSplitOptions.RemoveEmptyEntries).ToList().ToArray();
  6. string combineStr = &quot;&quot;;
  7. for (int i = 0; i &lt; splitted.Count()-1; i++)
  8. combineStr += splitted[i] += &quot;/&quot;;
  9. path = combineStr;
  10. print(&quot;results: &quot; + path);
  11. }

So everytime SomeFunc() is called;
<br>
first call is LivingOrganism/Animal/Bird/
<br>
second call is LivingOrganism/Animal/
<br>
third call is LivingOrganism/
<br>
fourth call is
<br>
which is what i intended to do.

however, is there actually a way to optimize this. ?
feels kinda ugly that i need to split the char first and the recombine them in forloop

答案1

得分: 2

你可以尝试使用LastIndexOfSubstring。类似以下的代码:

  1. void SomeFunc1()
  2. {
  3. string result;
  4. if (path.Length == 0)
  5. {
  6. result = path;
  7. }
  8. else
  9. {
  10. var lastIndexOf = path.LastIndexOf("/", path.Length - 2);
  11. result = lastIndexOf switch
  12. {
  13. >= 0 => path.Substring(0, lastIndexOf + 1),
  14. _ => string.Empty
  15. };
  16. }
  17. Console.WriteLine("results: " + result);
  18. path = result;
  19. }

请注意,System.IO提供了路径操作函数,所以你也可以尝试简化为:

  1. void SomeFunc1()
  2. {
  3. path = Path.GetDirectoryName(path);
  4. Console.WriteLine(path);
  5. }
英文:

You can try using LastIndexOf and Substring. Something like the following:

  1. void SomeFunc1()
  2. {
  3. string result;
  4. if (path.Length == 0)
  5. {
  6. result = path;
  7. }
  8. else
  9. {
  10. var lastIndexOf = path.LastIndexOf(&quot;/&quot;,path.Length - 2);
  11. result = lastIndexOf switch
  12. {
  13. &gt;= 0 =&gt; path.Substring(0, lastIndexOf + 1),
  14. _ =&gt; string.Empty
  15. };
  16. }
  17. Console.WriteLine(&quot;results: &quot; + result);
  18. path = result;
  19. }

Note that System.IO provides path manipulation functions so you can also try just:

  1. void SomeFunc1()
  2. {
  3. path = Path.GetDirectoryName(path);
  4. Console.WriteLine(path);
  5. }

答案2

得分: 0

这是一个应该能工作的代码片段。可能有更好的解决方案。

  1. string path = "LivingOrganism/Animal/Bird/Chicken.exe";
  2. public void SomeFunc()
  3. {
  4. for(int i=path.Length-1;i>=0;i--){
  5. if(path[i] == '/' || i==0){
  6. path = path.Remove(i);
  7. break;
  8. }
  9. }
  10. Console.WriteLine("结果: " + path);
  11. }
英文:

Here is a code snippet that should work. There are probably better solutions.

  1. string path = &quot;LivingOrganism/Animal/Bird/Chicken.exe&quot;;
  2. public void SomeFunc()
  3. {
  4. for(int i=path.Length-1;i&gt;=0;i--){
  5. if(path[i] == &#39;/&#39; || i==0){
  6. path = path.Remove(i);
  7. break;
  8. }
  9. }
  10. Console.WriteLine(&quot;results: &quot; + path);
  11. }

huangapple
  • 本文由 发表于 2023年8月8日 19:59:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76859347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定