I would like to remove the slash at the end of the URI, while keeping the query parameter.
For example:
I get an object of type URI as input with the value:
https://stackoverflow.com/questions/ask/
I would like to remove the last "/" to get:
https://stackoverflow.com/questions/ask
In some cases I may have parameters:
https://stackoverflow.com/questions/ask/?test=test1
I would like to get:
https://stackoverflow.com/questions/ask?test=test1
Thanks in advance.
string url1 = "https://stackoverflow.com/questions/ask/";
string url2 = "https://stackoverflow.com/questions/ask/?test=test1";
//remove slash at end
Console.WriteLine(url1.TrimEnd('/'));
//remove slash preceding query parameters
int last = url2.LastIndexOf('?');
url2 = url2.Remove(last - 1, 1);
Console.WriteLine(url2);
There probably is a way to search and replace the last slash using Regex as well, since there is Regex.Replace()
You can first check if the URL has any query parameters by checking latest index of ? character. If there is ?, take before that, remove last character (which is / that you don't want) and combine it again. If it doesn't have any query parameters, just remove the last character! You can create an extension method like this;
public static string RemoveSlash(this string url)
{
var queryIndex = url.LastIndexOf('?');
if (queryIndex >= 0)
{
var urlWithoutQueryParameters = url[..queryIndex];
if (urlWithoutQueryParameters.EndsWith("/"))
{
urlWithoutQueryParameters = urlWithoutQueryParameters[..^1];
}
var queryParemeters = url[queryIndex..];
return urlWithoutQueryParameters + queryParemeters;
}
else if (url.EndsWith("/"))
{
return url[..^1];
}
return url;
}
Then you can use it like this;
var url1 = "https://stackoverflow.com/questions/ask";
var url2 = "https://stackoverflow.com/questions/ask/";
Console.WriteLine(url1.RemoveSlash()); // https://stackoverflow.com/questions/ask
Console.WriteLine(url2.RemoveSlash()); // https://stackoverflow.com/questions/ask
var url3 = "https://stackoverflow.com/questions/ask/?test=test1";
var url4 = "https://stackoverflow.com/questions/ask?test=test1";
Console.WriteLine(url3.RemoveSlash()); // https://stackoverflow.com/questions/ask?test=test1
Console.WriteLine(url4.RemoveSlash()); // https://stackoverflow.com/questions/ask?test=test1
Related
I need to insert some string value after the last slash. I have such string value:
string url = "http://blog.loc/blog/news/sport/slug1_slug2_slug3-slug";
I need to get this value:
"http://blog.loc/blog/news/sport/hot_slug1_slug2_slug3-slug"
So, I need to insert hot_ (for example), after the last slash. Could anyone help me?
I know you asked for regex, but it's not really necessary in my opinion.
You can just use string.Insert:
string url = "http://blog.loc/blog/news/sport/slug1_slug2_slug3-slug";
url = url.Insert(url.LastIndexOf("/") + 1, "hot_");
url now holds the value: http://blog.loc/blog/news/sport/hot_slug1_slug2_slug3-slug
Regex Method :
string url = "http://blog.loc/blog/news/sport/slug1_slug2_slug3-slug";
var matches = Regex.Matches(url, "/");
var match = matches[matches.Count - 1];
string result = url.Insert(match.Index + 1, "hot_")
I have a list of string where one item is like, textItem1 = "Brown, Adam. (user)(admin)(Sales)" where I would always have to extract the text from the last pair of parentheses, which in this case will be Sales.
I tried the following:
string name = DDlistName.SelectedItem.ToString();
int start = name.IndexOf("(");
int end = name.IndexOf("(");
string result = name.Substring(start + 1, end - start - 1);
_UILabelPrintName.Text = result;
Problem: This always picks the text from first pair of parentheses, which in this case user.
Reading lots of similar question's answer I realised Regex might not be recommended in this case (not particularly succeeded either trying other codes). However any help with any short routine which can do the task will be really appreciated.
You need to use LastIndexOf instead of IndexOf, and check for a close parenthesis at the end.
string name = "Brown, Adam. (user)(admin)(Sales)";
int start = name.LastIndexOf("(");
int end = name.LastIndexOf(")");
string result = name.Substring(start + 1, end - start - 1);
Really you'd want to validate start and end to be sure that both parenthesis were found. LastIndexOf returns -1 if the character is not found.
And in order to handle nesting we need to search forward for the closing parenthesis after the location of the opening parenthesis.
string name = "Brown, Adam. (user)(admin)((Sales))";
int start = name.LastIndexOf('(');
int end = (start >= 0) ? name.IndexOf(')', start) : -1;
string result = (end >= 0) ? name.Substring(start + 1, end - start - 1) : "";
You can use the split function, breaking the string at the opening parenthesis. The last array element is the desired output with a tailing ")", which will then be removed.
var input = "Brown, Adam. (user)(admin)(Sales)";
// var input = DDlistName.SelectedItem.ToString();
var lastPick = input.Split(new[] { "(" }, StringSplitOptions.RemoveEmptyEntries).Last();
var output = lastPick.Substring(0, lastPick.Length - 1);
_UILabelPrintName.Text = output;
Another approach is to use a while loop with IndexOf. It cuts the input string as long as another "(" is found. If not more "(" are found, it takes the contents of the remaining string until the closing parenthesis ")":
int current = -1;
while(name.IndexOf("(") > 0)
{
name = name.Substring(name.IndexOf("(") + 1);
}
var end = name.IndexOf(")");
var output = name.Substring(0, end);
_UILabelPrintName.Text = output;
Or use LastIndexOf....
In my code behind in C# I have the following code. How do I change the replace so that only
the first occurance of www is replaced?
For example if the User enters www.testwww.com then I should be saving it as testwww.com.
Currently as per the below code it saves as www.com (guess due to substr code).
Please help. Thanks in advance.
private string FilterUrl(string url)
{
string lowerCaseUrl = url.ToLower();
lowerCaseUrl = lowerCaseUrl.Replace("http://", string.Empty).Replace("https://", string.Empty).Replace("ftp://", string.Empty);
lowerCaseUrl = lowerCaseUrl.Replace("www.", string.Empty);
string lCaseUrl = url.Substring(url.Length - lowerCaseUrl.Length, lowerCaseUrl.Length);
return lCaseUrl;
}
As Ally suggested. You are much better off using System.Uri. This also replaces the leading www as you wish.
private string FilterUrl(string url)
{
Uri uri = new UriBuilder(url).Uri; // defaults to http:// if missing
return Regex.Replace(uri.Host, "^www.", "") + uri.PathAndQuery;
}
Edit: The trailing slash is because of the PathAndQuery property. If there was no path you are left with the slash only. Just add another regex replace or string replace. Here's the regex way.
return Regex.Replace(uri.Host, "^www.", "") + Regex.Replace(uri.PathAndQuery, "/$", "");
I would suggest using indexOf(string) to find the first occurrence.
Edit: okay someone beat me to it ;)
You could use IndexOf like Felipe suggested OR do it the low tech way..
lowerCaseUrl = lowerCaseUrl.Replace("http://", string.Empty).Replace("https://", string.Empty).Replace("ftp://", string.Empty).Replace("http://www.", string.Empty).Replace("https://www.", string.Empty)
Would be interested to know what you're trying to achieve.
Came up with a cool static method, also works for replacing the first x occurrences:
public static string ReplaceOnce(this string s, string replace, string with)
{
return s.ReplaceCount(replace, with);
}
public static string ReplaceCount(this string s, string replace, string with, int howManytimes = 1)
{
if (howManytimes < 0) throw InvalidOperationException("can not replace a string less than zero times");
int count = 0;
while (s.Contains(replace) && count < howManytimes)
{
int position = s.IndexOf(replace);
s = s.Remove(position, replace.Length);
s = s.Insert(position, with);
count++;
}
return s;
}
The ReplaceOnce isn't necessary, just a simplifier. Call it like this:
string url = "http://www.stackoverflow.com/questions/www/www";
var urlR1 - url.ReplaceOnce("www", "xxx");
// urlR1 = "http://xxx.stackoverflow.com/questions/www/www";
var urlR2 - url.ReplaceCount("www", "xxx", 2);
// urlR2 = "http://xxx.stackoverflow.com/questions/xxx/www";
NOTE: this is case-sensitive as it is written
The Replace method will change all content of the string. You have to locate the piece you want to remove using IndexOf method, and remove using Remove method of string. Try something like this:
//include the namespace
using System.Globalization;
private string FilterUrl(string url)
{
// ccreate a Comparer object.
CompareInfo myCompare = CultureInfo.InvariantCulture.CompareInfo;
// find the 'www.' on the url parameter ignoring the case.
int position = myCompare.IndexOf(url, "www.", CompareOptions.IgnoreCase);
// check if exists 'www.' on the string.
if (position > -1)
{
if (position > 0)
url = url.Remove(position - 1, 5);
else
url = url.Remove(position, 5);
}
//if you want to remove http://, https://, ftp://.. keep this line
url = url.Replace("http://", string.Empty).Replace("https://", string.Empty).Replace("ftp://", string.Empty);
return url;
}
Edits
There was a part in your code that is removing a piece of string. If you just want to remove the 'www.' and 'http://', 'https://', 'ftp://', take a look the this code.
This code also ignore the case when it compares the url parameter and what you have been findind, on case, 'www.'.
I am working on string maniplations using regex.
Source: string value = #"/webdav/MyPublication/Building%20Blocks/folder0/folder1/content_1.xml";
output required:
Foldername: folder1
content name: content
folderpath:/webdav/MyPublication/Building%20Blocks/folder0/folder1/
I am new to this, can any one say how it can be done using regex.
Thank you.
The rules you need seem to be the following:
Folder name = last string preceding a '/' character but not containing a '/' character
content name = last string not containing a '/' character until (but not including) a '_' or '.' character
folderpath = same as folder name except it can contain a '/' character
Assuming the rules above - you probably want this code:
string value = #"/webdav/MyPublication/Building%20Blocks/folder0/folder1/content_1.xml";
var foldernameMatch = Regex.Match(value, #"([^/]+)/[^/]+$");
var contentnameMatch = Regex.Match(value, #"([^/_\.]+)[_\.][^/]*$");
var folderpathMatch = Regex.Match(value, #"(.*/)[^/]*$");
if (foldernameMatch.Success && contentnameMatch.Success && folderpathMatch.Success)
{
var foldername = foldernameMatch.Groups[1].Value;
var contentname = contentnameMatch.Groups[1].Value;
var folderpath = folderpathMatch.Groups[1].Value;
}
else
{
// handle bad input
}
Note that you can also combine these to become one large regex, although it can be more cumbersome to follow (if it weren't already):
var matches = Regex.Match(value, #"(.*/)([^/]+)/([^/_\.]+)[_\.][^/]*$");
if (matches.Success)
{
var foldername = matches.Groups[2].Value;
var contentname = matches.Groups[3].Value;
var folderpath = matches.Groups[1].Value + foldername + "/";
}
else
{
// handle bad input
}
You could use named captures, but you're probably better off (from a security and implementation aspect) just using the Uri class.
I agree with Jeff Moser on this one, but to answer the original question, I believe the following regular expression would work:
^(\/.+\/)(.+?)\/(.+?)\.
edit: Added example.
var value = "/webdav/MyPublication/Building%20Blocks/folder0/folder1/content_1.xml";
var regex = Regex.Match(value, #"^(\/.+\/)(.+?)\/(.+?)\.");
// check if success
if (regex.Success)
{
// asssign the values from the regular expression
var folderName = regex.Groups[2].Value;
var contentName = regex.Groups[3].Value;
var folderPath = regex.Groups[1].Value;
}
I have the string: "http://schemas.xmlsoap.org/ws/2004/09/transfer/Get". I want to trim everything from the last slash, so I just remain with "Get".
var s = "http://schemas.xmlsoap.org/ws/2004/09/transfer/Get";
s = s.Substring(s.LastIndexOf("/") + 1);
You could use the LastIndexOf method to get the position of the last / in the string and pass that into the Substring method as how many characters you want to trim off the string. That should leave you with the Get at the end.
[TestMethod]
public void ShouldResultInGet()
{
string url = "http://schemas.xmlsoap.org/ws/2004/09/transfer/Get";
int indexOfLastSlash = url.LastIndexOf( '/' ) + 1; //don't want to include the last /
Assert.AreEqual( "Get", url.Substring( indexOfLastSlash ) );
}
Use String.LastIndexOf to get the last forward slash
http://msdn.microsoft.com/en-us/library/ms224422.aspx
URI alternative if your using the well formed /Get /Put /Delete etc
var uri = new System.Uri("http://schemas.xmlsoap.org/ws/2004/09/transfer/Get");
string top = Path.GetFileName(uri.LocalPath);
try
int indexOfLastSlash = url.LastIndexOf( '/' ) + 1;
string s = url.Remove(0, indexOfLastSlash);
Assert.AreEqual( "Get", s );
this removes all data before & including the last '/'.
works fine here.