英文:
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 = "LivingOrganism/Animal/Bird/Chicken.exe";
public void SomeFunc()
{
//count the string from the back until it hit the seperator
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);
}
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
你可以尝试使用LastIndexOf
和Substring
。类似以下的代码:
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("/",path.Length - 2);
result = lastIndexOf switch
{
>= 0 => path.Substring(0, lastIndexOf + 1),
_ => string.Empty
};
}
Console.WriteLine("results: " + 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 = "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("results: " + path);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论