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

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

Get the string backward until it hit certain character

问题

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

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

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

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

    path = combineStr;
    print("results: " + path);
}

所以每次调用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.

string path = &quot;LivingOrganism/Animal/Bird/Chicken.exe&quot;;
public void SomeFunc()
{
	//count the string from the back until it hit the seperator
        string[] splitted = path.Split(new string[] { &quot;/&quot; }, StringSplitOptions.RemoveEmptyEntries).ToList().ToArray();
        string combineStr = &quot;&quot;;
        for (int i = 0; i &lt; splitted.Count()-1; i++)
                combineStr += splitted[i] += &quot;/&quot;;

	    path = combineStr;
        print(&quot;results: &quot; + path);
}

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。类似以下的代码:

void SomeFunc1()
{
    string result;
    if (path.Length == 0)
    {
        result = path;
    }
    else
    {
        var lastIndexOf = path.LastIndexOf("/", path.Length - 2);
        result = lastIndexOf switch
        {
            >= 0 => path.Substring(0, lastIndexOf + 1),
            _ => string.Empty
        };
    }
    Console.WriteLine("results: " + result);
    path = result;
}

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

void SomeFunc1()
{
    path = Path.GetDirectoryName(path);
    Console.WriteLine(path);
}
英文:

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

void SomeFunc1()
{
    string result;
    if (path.Length == 0)
    {
        result = path;
    }
    else
    {
        var lastIndexOf = path.LastIndexOf(&quot;/&quot;,path.Length - 2);
        result = lastIndexOf switch
        {
            &gt;= 0 =&gt; path.Substring(0, lastIndexOf + 1),
            _ =&gt; string.Empty
        };
    }
    Console.WriteLine(&quot;results: &quot; + result);
    path = result;
}

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

void SomeFunc1()
{
    path = Path.GetDirectoryName(path);
    Console.WriteLine(path);
}

答案2

得分: 0

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

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

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

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

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:

确定