Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 拆分并替换字符串_C#_Asp.net - Fatal编程技术网

C# 拆分并替换字符串

C# 拆分并替换字符串,c#,asp.net,C#,Asp.net,我有一个格式为的字符串: string path="/fm/Templates/testTemplate/css/article.jpg"; 我想用第二个“/”将其拆分,并用“/”替换为“\\”,以得到这样的结果 string newPath="\\Templates\\testTemplate\\css\\article.jpg"; 我的路径是动态创建的,因此文件夹层次结构不是固定的。 最好的方法是什么。我可以先用/拆分字符串路径,然后转到“循环”将其重新浓缩,并用“/”重新拼合到“\\”

我有一个格式为的字符串:

string path="/fm/Templates/testTemplate/css/article.jpg";
我想用第二个“/”将其拆分,并用“/”替换为“\\”,以得到这样的结果

string newPath="\\Templates\\testTemplate\\css\\article.jpg";
我的
路径是动态创建的,因此文件夹层次结构不是固定的。
最好的方法是什么。我可以先用
/
拆分
字符串路径
,然后转到“循环”将其重新浓缩,并用“/”重新拼合到“\\”或者我缺少的任何简单方法。

你不能直接使用吗?

你不能直接使用吗?

试试这个:

public static int nthOccurrence(String str, char c, int n)
{
    int pos = str.IndexOf(c, 0);
    while (n-- > 0 && pos != -1)
        pos = str.IndexOf(c, pos + 1);
    return pos;
}    

string path = "/fm/Templates/testTemplate/css/article.jpg";

int index = nthOccurrence(path, '/', 1);
string newpath = path.Substring(index).Replace("/", "\\");
输出:
“\\Templates\\testTemplate\\css\\article.jpg”
尝试以下方法:

public static int nthOccurrence(String str, char c, int n)
{
    int pos = str.IndexOf(c, 0);
    while (n-- > 0 && pos != -1)
        pos = str.IndexOf(c, pos + 1);
    return pos;
}    

string path = "/fm/Templates/testTemplate/css/article.jpg";

int index = nthOccurrence(path, '/', 1);
string newpath = path.Substring(index).Replace("/", "\\");

输出:
“\\Templates\\testTemplate\\css\\article.jpg”
从路径中删除第一项后,可以创建新字符串。以下内容将为您提供所需的结果。它可以被优化

您可以尝试:

  • 拆分char
    /
    上的字符串并删除空条目
  • 忽略拆分数组中的第一项后获取新数组
  • 使用
    string.Join
    创建以
    \\
    作为分隔符的新字符串
代码如下:

string path = "/fm/Templates/testTemplate/css/article.jpg";
string[] temp = path.Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
var newTemp = temp.AsEnumerable().Where((r, index) => index > 0);
string newPath2 = string.Join("\\", newTemp);

从路径中删除第一项后,可以创建新字符串。以下内容将为您提供所需的结果。它可以被优化

您可以尝试:

  • 拆分char
    /
    上的字符串并删除空条目
  • 忽略拆分数组中的第一项后获取新数组
  • 使用
    string.Join
    创建以
    \\
    作为分隔符的新字符串
代码如下:

string path = "/fm/Templates/testTemplate/css/article.jpg";
string[] temp = path.Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
var newTemp = temp.AsEnumerable().Where((r, index) => index > 0);
string newPath2 = string.Join("\\", newTemp);

如果您正在处理路径的各个段(而不仅仅是替换斜杠的方向),System.Uri类将提供帮助(.segments属性)

例如来自


如果您正在处理路径的各个段(而不仅仅是替换斜杠的方向),System.Uri类将提供帮助(.segments属性)

例如来自


您可以使用以下代码

string path="/fm/Templates/testTemplate/css/article.jpg"; 
string newpath = path.Replace("/fm", string.Empty).Replace("/","\\"); 

谢谢

您可以使用以下代码

string path="/fm/Templates/testTemplate/css/article.jpg"; 
string newpath = path.Replace("/fm", string.Empty).Replace("/","\\"); 
谢谢你试试这个

string path = "/fm/Templates/testTemplate/css/article.jpg";
string[] oPath = path.Split('/');
string newPath = @"\\" + string.Join(@"\\", oPath, 2, oPath.Length - 2);
Console.WriteLine(newPath);
试试这个

string path = "/fm/Templates/testTemplate/css/article.jpg";
string[] oPath = path.Split('/');
string newPath = @"\\" + string.Join(@"\\", oPath, 2, oPath.Length - 2);
Console.WriteLine(newPath);

您的字符串操作是否真的对时间如此关键,以至于您需要立即执行所有操作?只需将其分解为几个部分就容易得多:

// First remove the leading /../ section:
path = Regex.Replace("^/[^/]*/", "", path);

// Then replace any remaining /s with \s:
path = path.Replace("/", "\\");

您的字符串操作是否真的对时间如此关键,以至于您需要立即执行所有操作?只需将其分解为几个部分就容易得多:

// First remove the leading /../ section:
path = Regex.Replace("^/[^/]*/", "", path);

// Then replace any remaining /s with \s:
path = path.Replace("/", "\\");

string.Replace(“/”,“\\”)?如果处理来自公共web的路径,听起来像是引入了一些安全漏洞。如果我是某个入侵者,我会尝试“/fm/./../Windows/System32/drivers/etc/hosts…”,
string.Replace(“/”,“\\”)?如果处理来自公共web的路径,听起来像是引入了一些安全漏洞。如果我想成为入侵者,我会尝试“/fm/。/../Windows/System32/drivers/etc/hosts”…我想先去掉“/fm/”,然后再替换?我想你对一个简单的问题想得太多了。只需先删除/fm/然后替换其余的“/”newPath=path;它是动态的,因此可能会出现
/am/
而不是
/fm/
,这是一个真正的问题?如果字符数相同,即2(am,fm),则只需在开始时删除4个字符,其中包括/too。我想先去掉“/fm/”,然后替换?我认为对于一个简单的问题,您考虑得太多了。只需先删除/fm/然后替换其余的“/”newPath=path;它是动态的,因此可能会出现
/am/
而不是
/fm/
,这是一个真正的问题?如果字符数相同,即2(am,fm)然后只需删除开头的4个字符,其中包括/too。
/fm/
仍在那里我想摆脱它?更新了我的答案..现在它可能会帮助你..@shrekhanal
/fm/
仍在那里我想摆脱它?更新了我的答案..现在它可能会帮助你..@shrekhanal