Reputation:
I am having trouble on how i should trim this string. I want everything after the last '/' to be deleted, but there are multiple '/'.
Example:
From:http://stackoverflow.com/questions/ask/randomcrap.html
To: http://stackoverflow.com/questions/ask/
Upvotes: 0
Views: 936
Reputation: 12683
String trimming can be done using many of the built in string utlitites.
First you want the LastIndexOf
the "/" character in the string, and then you want use the Substring
method from the index of 0
(start of the string) to the index of the /
character. Now this will return everything to the last /
if you want to include the slash you have to add one more index.
static string getToLastSlash(string inString)
{
var index = inString.LastIndexOf('/');
if (index == -1)
throw new Exception("/ not found in string");
return inString.Substring(0, index + 1);
}
The LastIndexOf()
method will return the last index of a specific character or string provided. If the LastIndexOf()
method does not evaluate a match the result will be -1
. Given the fact that you could have a non-match, you must first check if the match result is -1
. In the above method we simply throw an exception however your application may have an alternative method to handle this situation.
If the match is greater that -1
then you can assume a match was found. When a match was found you can use the Substring()
method to parse all the characters in the string starting at position 0
to the index+1
.
Upvotes: 10
Reputation: 186688
If you want to find out the last '/' character, just use String LastIndexOf method:
String text = @"http://stackoverflow.com/questions/ask/randomcrap.html";
String result = text;
int index = text.LastIndexOf('/'); // <- last '/' if any
if (index >= 0) // <- if there's '/' within the string, trim it
result = text.Substring(0, index + 1);
...
Console.Out.Write(result); // <- http://stackoverflow.com/questions/ask/
Upvotes: 0
Reputation: 63065
I think you can do this by using Uri class
Uri parent = new Uri(uri, ".");
Upvotes: 2
Reputation: 17186
void Main()
{
Console.WriteLine (TrimLastPart("http://stackoverflow.com/questions/ask/randomcrap.html"));
Console.WriteLine (TrimLastPart("http://stackoverflow.com/questions/ask//randomcrap.html"));
}
enum State
{
SlashNotEncountered,
SlashEncountered,
BuildingResult
}
string TrimLastPart(string input)
{
string result = string.Empty;
State state = State.SlashNotEncountered;
foreach (var c in input.ToCharArray().Reverse())
{
switch (state)
{
case State.SlashNotEncountered:
if(c == '/')
{
state = State.SlashEncountered;
}
break;
case State.SlashEncountered:
if(c != '/')
{
state = State.BuildingResult;
result = c.ToString();
}
break;
case State.BuildingResult:
result = c + result;
break;
}
}
return result + "/";
}
Output:
http://stackoverflow.com/questions/ask/
http://stackoverflow.com/questions/ask/
Upvotes: 0
Reputation: 629
You can Split
the char '/'
then convert it to a List<string>
and remove the last item and finally reunite the parts using string.Join
var url = "http://stackoverflow.com/questions/ask/randomcrap.html";
var b = url.Replace("http://", "").Split('/').ToList(); //replace "html://" to avoid problems with the split
b.RemoveAt(b.Count-1); //last index
var result = "http://" + string.Join("/", b) + "/";
result
equals to "http://stackoverflow.com/questions/ask/"
This will allow you to remove any part you wish.
Don't forget you could also use b.RemoveRange(int index, int count)
;
Upvotes: 0
Reputation: 8879
Most simple way would probably be
string myURL = "http://stackoverflow.com/questions/ask/randomcrap.html";
string str = myURL.Substring(0, myURL.LastIndexOf('/'));
Console.WriteLine(str);
which will output
http://stackoverflow.com/questions/ask
Upvotes: 1
Reputation: 7719
I would use a regular expression Replace with an end ($
) anchor.
var res = Regex.Replace(input,
@"[^/]+$", // match all the non-slashes anchored to the end of input
""); // replace any matched characters with nothing
// and make sure to use "res"
I prefer to use a regular expression for tasks like this - because I find it simpler and it can avoid an extra conditional guard "for free" - but make sure to understand the mechanics of the approach used.
Upvotes: 3
Reputation: 69372
You can use LastIndexOf like this
string str = "http://stackoverflow.com/questions/ask/randomcrap.html";
string final = RemoveAfterLastChar(str, '/');
public string RemoveAfterLastChar(string input, char c)
{
var index = input.LastIndexOf(c);
if (index != -1)
return input.Substring(0, index + 1);
else
return input;
}
Or, if the format is always going to be the same as your example, you could use Path.GetDirectoryName.
input = Path.GetDirectoryName(input);
Upvotes: 0
Reputation: 19203
Combining string.Substring
and string.LastIndexOf
should do the trick.
Upvotes: 2