[英]How can i remove the part “http://” from a string?
I have this method: 我有这个方法:
private List<string> offline(string targetDirectory)
{
if (targetDirectory.Contains("http://"))
{
MessageBox.Show("true");
}
DirectoryInfo di = new DirectoryInfo(targetDirectory);
List<string> directories = new List<string>();
try
{
string[] dirs = Directory.GetDirectories(targetDirectory,"*.*",SearchOption.TopDirectoryOnly);
for (int i = 0; i < dirs.Length; i++)
{
string t = "http://" + dirs[i];
directories.Add(t);
}
}
catch
{
MessageBox.Show("hgjghj");
}
return directories;
}
This is the part: 这是部分:
if (targetDirectory.Contains("http://"))
{
MessageBox.Show("true");
}
I'm getting a directory which give me all the directories in this directory and I'm adding to each directory the string "http://"
. 我正在获取一个目录,它给我这个目录中的所有目录,并且我在每个目录中添加字符串"http://"
。
The problem is when next time a directory is getting to the function its coming with "http://"
问题是,当下一次目录到达该函数时,它将以"http://"
For example: http://c:\\\\
or http://c:\\\\windows
例如: http://c:\\\\
或http://c:\\\\windows
And then the line 然后就行了
DirectoryInfo di = new DirectoryInfo(targetDirectory); // throws exception.
So I want that each time a directory is getting to the function to check if it starts with "http://"
in the beginning, strip the "http://"
part, get all the directories, and then add to each directory "http://"
like now. 所以我希望每次目录到达函数时检查它是否以"http://"
开头,剥离"http://"
部分,获取所有目录,然后添加到每个目录"http://"
就像现在一样。
How can I remove "http://"
? 如何删除"http://"
?
I would be stricter than using Contains
- I'd use StartsWith
, and then Substring
: 我会比使用Contains
更严格 - 我使用StartsWith
,然后使用Substring
:
if (targetDirectory.StartsWith("http://"))
{
targetDirectory = targetDirectory.Substring("http://".Length);
}
Or wrap it in a helper method: 或者将其包装在辅助方法中:
public static string StripPrefix(string text, string prefix)
{
return text.StartsWith(prefix) ? text.Substring(prefix.Length) : text;
}
It's not clear to me why you're putting the http://
as a prefix anyway though, to be honest. 我不清楚为什么你把http://
作为前缀无论如何,说实话。 I can't see how you'd expect a directory name prefixed with http://
to be a valid URL. 我看不出你希望以http://
为前缀的目录名是一个有效的URL。 Perhaps if you could explain why you're doing it, we could suggest a better approach. 也许如果你能解释你为什么这样做,我们可以建议一个更好的方法。
(Also, I really hope you don't have a try/catch block like that in your real code, and that normally you follow .NET naming conventions.) (另外,我真的希望你的实际代码中没有像这样的try / catch块,通常你遵循.NET命名约定。)
The problem is how can i remove the http:// ? 问题是如何删除http://?
You may use string.Replace , and replace the string with an empty string. 您可以使用string.Replace ,并用空字符串替换该字符串。
targetDirectory = targetDirectory.Replace("http://","");
or 要么
targetDirectory = targetDirectory.Replace("http://",string.Empty);
both of them are same 两者都是一样的
Try this: 试试这个:
if(example.StartsWith("http://"))
{
example.substring(7);
}
You can always use the String.Replace to remove / replace characters in the string. 您始终可以使用String.Replace删除/替换字符串中的字符。 Exampel: Exampel:
targetDirectory = targetDirectory.Replace("http://", string.Empty);
And you can check if the string begins with Http:// by doing 你可以检查字符串是否以Http://开头
if(targetDirectory.StartsWith("http://"))
You can use the replace characters in the string by string.Replace
您可以通过string.Replace
在字符串中使用替换字符
if (targetDirectory.Contains("http://"))
{
targetDirectory = targetDirectory.Replace("http://",string.Empty);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.