I have a the following path which I need to convert to URL.
string path = @"\\TestServer\User_Attachments$\Data\Reference\Input\Test.png";
I'm trying to replace the replace the special chars in this string. So
\\\\ will become
// with an
https added at the front ie
https://` \\
will become /
User_Attachments$
will become User_Attachments
The final string should look like
string url = "https://TestServer/User_Attachments/Data/Reference/Input/Test.png"
To achieve this I've come up with the following regex
string pattern = @"^(.{2})|(\\{1})|(\${1})";
I then match using the Matches()
method as:
var match = Regex.Matches(path, pattern);
My question is how can I check to see if the match is success and replace the appropriate value at the appropriate group and have a final url
string as I mentioned above.
Here is the link to the regex
As mentioned above, i'd go for a simple Replace
:
string path = @"\\TestServer\User_Attachments$\Data\Reference\Input\Test.png";
var url = path.Replace(@"\\", @"https://").Replace(@"\", @"/").Replace("$", string.Empty);
// note if you want to get rid of all special chars you would do the last bit differently
For example, taken out of one of these SO answers here: How do I remove all non alphanumeric characters from a string except dash?
// assume str contains the data with special chars
char[] arr = str.ToCharArray();
arr = Array.FindAll<char>(arr, (c => (char.IsLetterOrDigit(c)
|| char.IsWhiteSpace(c)
|| c == '-'
|| c == '_')));
str = new string(arr);
You can do it like this
string path = @"\\TestServer\User_Attachments$\Data\Reference\Input\Test.png";
string actualUrl=path.Replace("\\","https://").Replace("\","/")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.