Get page name from URI, substring between two characters - c#

I want to get the page name from a URI for instance if I have
"/Pages/Alarm/AlarmClockPage.xaml"
I want to get AlarmClockPage
I tried
//usage GetSubstring("/", ".", "/Pages/Alarm/AlarmClockPage.xaml")
public static string GetSubstring(string a, string b, string c)
{
string str = c.Substring((c.IndexOf(a) + a.Length),
(c.IndexOf(b) - c.IndexOf(a) - a.Length));
return str;
}
But because the string being search may contain one or more forward slashes, I don't think this method work in such case.
So how do I consider the multiple forward slashes that may present?

Why don't you use method which is already in the framework?
System.IO.Path.GetFileNameWithoutExtension(#"/Pages/Alarm/AlarmClockPage.xaml");

If you only want to use string functions, you may try:
var startIdx = pathString.LastIndexOf(#"/");
var endIdx = pathString.LastIndexOf(".");
if(endIdx!=-1)
{
fileName = pathString.Substring(startIdx,endIdx);
}
else
{
fileName = pathString.Substring(startIdx);
}

It gives file name from a given file path. try this
string pageName = System.IO.Path.GetFileName(#"/Pages/Alarm/AlarmClockPage.xaml");

Related

How to split a defined string from another string and get the first item after split

I have this string D:\ASN\Documents\ENU\LO\ANL\File\05003ede-59bf-45c6-bb57-a6111e9f18e0\linux-cheat-sheet.pdf and I want to exclude this string D:\ASN\Documents\ENU\LO from the above string and then get the first string(in this case ANL)after the split.
I tried something like this:
string fullpath = "D:\\ASN\\Documents\\ENU\\LO\\ANL\\File\\05003ede-59bf-45c6-bb57-a6111e9f18e0\\linux-cheat-sheet.pdf"
string[] sep = new string[]{"D:\\ASN\\Documents\\ENU\\LO"};
string [] result = fullpath.split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in result)
{
Console.Write(s.Substring(s.IndexOf(#"\") + 1));
}
But this is giving me ANL\File\05003ede-59bf-45c6-bb57-a6111e9f18e0\linux-cheat-sheet.pdf". Instead I need only ANL. How can this be achieved? Is there any other way to get this instead of this way.
TIA
var result = fullpath.Replace(samplePath, "").Split('\\')[1];
You can replace the first part (samplePath) with nothing, removing it (or you could use Substring to get the second part of the fullPath, counting the characters of samplePath), and then Split the result on '\', getting the second occurrence, which is the result you expect.
Here's a working version: https://dotnetfiddle.net/k4tfGP
Can you do a split on the second string when it sees the \?
String sampleString = "ANL\File\05003ede-59bf-45c6-bb57-a6111e9f18e0\linux-cheat-sheet.pdf"";
String[] stringArray = sampleString.split("\");
String wantedString = stringArray[0];
This is not what split() is intended for. split() is generally used to divide your string into multiple sections based on a separator. In your case, you might have wanted to use it to separate the sub-folders by splitting on '\'.
But you want something else -- to remove a section of text. If you know that the text will always be at the start, try this:
string result = fullpath.Substring("D:\\ASN\\Documents\\ENU\\LO".Length);
This will return the original string, minus the first X characters, where X is exactly the length of the string you want to remove.
string fullpath =
"D:\\ASN\\Documents\\ENU\\LO\\ANL\\File\\05003ede-59bf-45c6-bb57-a6111e9f18e0\\linux-cheat-sheet.pdf";
string[] sep = new string[] {"D:\\ASN\\Documents\\ENU\\LO"};
string[] result = fullpath.Split(sep, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in result)
{
Console.Write(s.Substring(s.IndexOf(#"\") + 1, s.IndexOf(#"\", 2) - 1));
}
String.IndexOf will get you the index of the first, but has overloads giving a starting point. So In this example, I have given the starting point as "2" as your path contains "\\" always.
string basepath = "D:\\ASN\\Documents\\ENU\\LO\\";
string fullpath = "D:\\ASN\\Documents\\ENU\\LO\\ANL\\File\\05003ede-59bf-45c6-bb57-a6111e9f18e0\\linux-cheat-sheet.pdf";
fullpath = fullpath.Replace(basepath, "");
string returnValue = fullpath.Remove(fullpath.IndexOf("\\"), fullpath.Length-fullpath.IndexOf("\\"));
Worked here...

How to get the second to last directory in a path string in C#

For example,
string path = #"C:\User\Desktop\Drop\images\";
I need to get only #"C:\User\Desktop\Drop\
Is there any easy way of doing this?
You can use the Path and Directory classes:
DirectoryInfo parentDir = Directory.GetParent(Path.GetDirectoryName(path));
string parent = parentDir.FullName;
Note that you would get a different result if the path doesn't end with the directory-separator char \. Then images would be understood as filename and not as directory.
You can also use a subsequent call of Path.GetDirectoryName
string parent = Path.GetDirectoryName(Path.GetDirectoryName(path));
This behaviour is documented here:
Because the returned path does not include the DirectorySeparatorChar
or AltDirectorySeparatorChar, passing the returned path back into the
GetDirectoryName method will result in the truncation of one folder
level per subsequent call on the result string. For example, passing
the path "C:\Directory\SubDirectory\test.txt" into the
GetDirectoryName method will return "C:\Directory\SubDirectory".
Passing that string, "C:\Directory\SubDirectory", into
GetDirectoryName will result in "C:\Directory".
This will return "C:\User\Desktop\Drop\" e.g. everything but the last subdir
string path = #"C:\User\Desktop\Drop\images";
string sub = path.Substring(0, path.LastIndexOf(#"\") + 1);
Another solution if you have a trailing slash:
string path = #"C:\User\Desktop\Drop\images\";
var splitedPath = path.Split('\\');
var output = String.Join(#"\", splitedPath.Take(splitedPath.Length - 2));
var parent = "";
If(path.EndsWith(System.IO.Path.DirectorySeparatorChar) || path.EndsWith(System.IO.Path.AltDirectorySeparatorChar))
{
parent = Path.GetDirectoryName(Path.GetDirectoryName(path));
parent = Directory.GetParent(Path.GetDirectoryName(path)).FullName;
}
else
parent = Path.GetDirectoryName(path);
As i commented GetDirectoryName is self collapsing it returns path without tralling slash - allowing to get next directory.Using Directory.GetParent for then clouse is also valid.
Short Answer :)
path = Directory.GetParent(Directory.GetParent(path)).ToString();
Example on the bottom of the page probably will help:
http://msdn.microsoft.com/en-us/library/system.io.path.getdirectoryname(v=vs.110).aspx
using System;
namespace Programs
{
public class Program
{
public static void Main(string[] args)
{
string inputText = #"C:\User\Desktop\Drop\images\";
Console.WriteLine(inputText.Substring(0, 21));
}
}
}
Output:
C:\User\Desktop\Drop\
There is probably some simple way to do this using the File or Path classes, but you could also solve it by doing something like this (Note: not tested):
string fullPath = "C:\User\Desktop\Drop\images\";
string[] allDirs = fullPath.split(System.IO.Path.PathSeparator);
string lastDir = allDirs[(allDirs.length - 1)];
string secondToLastDir= allDirs[(allDirs.length - 2)];
// etc...

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://".
The problem is when next time a directory is getting to the function its coming with "http://"
For example: http://c:\\ or 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.
How can I remove "http://"?
I would be stricter than using Contains - I'd use StartsWith, and then 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. I can't see how you'd expect a directory name prefixed with http:// to be a valid 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.)
The problem is how can i remove the http:// ?
You may use string.Replace, and replace the string with an empty string.
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.
Exampel:
targetDirectory = targetDirectory.Replace("http://", string.Empty);
And you can check if the string begins with Http:// by doing
if(targetDirectory.StartsWith("http://"))
You can use the replace characters in the string by string.Replace
if (targetDirectory.Contains("http://"))
{
targetDirectory = targetDirectory.Replace("http://",string.Empty);
}

How to make filenames web safe using c#

This is not about encoding URLs its more to do with a problem I noticed where you can have a valid filename on IIS sucha as "test & test.jpg" but this cannot be downloaded due to the & causing an error. There are other characters that do this also that are valid in windows but not for web.
My quick solution is to change the filename before saving using a regex below...
public static string MakeFileNameWebSafe(string fileNameIn)
{
string pattern = #"[^A-Za-z0-9. ]";
string safeFilename = System.Text.RegularExpressions.Regex.Replace(fileNameIn, pattern, string.Empty);
if (safeFilename.StartsWith(".")) safeFilename = "noname" + safeFilename;
return safeFilename;
}
but I was wondering if there were any better built in ways of doing this.
Built-in I don't know about.
What you can do is, like you say, scan the original filename and generate a Web-safe version of it.
For such Web-safe versions, you can make it appear like slugs in blogs and blog categories (these are search engine-optimized):
Only lowercase characters
Numbers are allowed
Dashes are allowed
Spaces are replaced by dashes
Nothing else is allowed
Possibly you could replace "&" by "-and-"
So "test & test.jpg" would translate to "test-and-test.jpg".
Just looking back at this question since its fairly popular. Just though I would post my current solution up here with various overloads for anyone who wants it..
public static string MakeSafeFilename(string filename, string spaceReplace)
{
return MakeSafeFilename(filename, spaceReplace, false, false);
}
public static string MakeSafeUrlSegment(string text)
{
return MakeSafeUrlSegment(text, "-");
}
public static string MakeSafeUrlSegment(string text, string spaceReplace)
{
return MakeSafeFilename(text, spaceReplace, false, true);
}
public static string MakeSafeFilename(string filename, string spaceReplace, bool htmlDecode, bool forUrlSegment)
{
if (htmlDecode)
filename = HttpUtility.HtmlDecode(filename);
string pattern = forUrlSegment ? #"[^A-Za-z0-9_\- ]" : #"[^A-Za-z0-9._\- ]";
string safeFilename = Regex.Replace(filename, pattern, string.Empty);
safeFilename = safeFilename.Replace(" ", spaceReplace);
return safeFilename;
}
I think you are referring to the "A potentially dangerous Request.Path value was detected from the client (%)" error which Asp.Net throws for paths which include characters which might indicate cross site scripting attempts:
there is a good article on how to work around this:
http://www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsAnglebracketsAndOtherNaughtyThingsInTheASPNETIISRequestURL.aspx
Here's the one I use:
public static string MakeFileNameWebSafe(string path, string replace, string other)
{
var folder = System.IO.Path.GetDirectoryName(path);
var name = System.IO.Path.GetFileNameWithoutExtension(path);
var ext = System.IO.Path.GetExtension(path);
if (name == null) return path;
var allowed = #"a-zA-Z0-9" + replace + (other ?? string.Empty);
name = System.Text.RegularExpressions.Regex.Replace(name.Trim(), #"[^" + allowed + "]", replace);
name = System.Text.RegularExpressions.Regex.Replace(name, #"[" + replace + "]+", replace);
if (name.EndsWith(replace)) name = name.Substring(0, name.Length - 1);
return folder + name + ext;
}
If you are not concerned to keep the original name perhaps you could just replace the name with a guid?

Auto quotes around string in c# - build in method?

Is there some build in method that add quotes around string in c# ?
Do you mean just adding quotes? Like this?
text = "\"" + text + "\"";
? I don't know of a built-in method to do that, but it would be easy to write one if you wanted to:
public static string SurroundWithDoubleQuotes(this string text)
{
return SurroundWith(text, "\"");
}
public static string SurroundWith(this string text, string ends)
{
return ends + text + ends;
}
That way it's a little more general:
text = text.SurroundWithDoubleQuotes();
or
text = text.SurroundWith("'"); // For single quotes
I can't say I've needed to do this often enough to make it worth having a method though...
string quotedString = string.Format("\"{0}\"", originalString);
Yes, using concatenation and escaped characters
myString = "\"" + myString + "\"";
Maybe an extension method
public static string Quoted(this string str)
{
return "\"" + str + "\"";
}
Usage:
var s = "Hello World"
Console.WriteLine(s.Quoted())
No but you can write your own or create an extension method
string AddQuotes(string str)
{
return string.Format("\"{0}\"", str);
}
Using Escape Characters
Just prefix the special character with a backslash, which is known as an escape character.
Simple Examples
string MyString = "Hello";
Response.Write(MyString);
This would print:
Hello
But:
string MyString = "The man said \"Hello\"";
Response.Write(MyString);
Would print:
The man said "Hello"
Alternative
You can use the useful # operator to help escape strings, see this link:
http://www.kowitz.net/archive/2007/03/06/the-c-string-literal
Then, for quotes, you would use double quotes to represent a single quote. For example:
string MyString = #"The man said ""Hello"" and went on his way";
Response.Write(MyString);
Outputs:
The man said "Hello" and went on his way
I'm a bit C# of a novice myself, so have at me, but I have this in a catch-all utility class 'cause I miss Perl:
// overloaded quote - if no quote chars spec'd, use ""
public static string quote(string s) {
return quote(s, "\"\"");
}
// quote a string
// q = two quote chars, like "", '', [], (), {} ...
// or another quoted string (quote-me-like-that)
public static string quote(string s, string q) {
if(q.Length == 0) // no quote chars, use ""
q = "\"\"";
else if(q.Length == 1) // one quote char, double it - your mileage may vary
q = q + q;
else if(q.Length > 2) // longer string == quote-me-like-that
q = q.Substring(0, 1) + q.Substring(q.Length - 1, 1);
if(s.Length == 0) // nothing to quote, return empty quotes
return q;
return q[0] + s + q[1];
}
Use it like this:
quote("this with default");
quote("not recommended to use one char", "/");
quote("in square brackets", "[]");
quote("quote me like that", "{like this?}");
Returns:
"this with default"
/not recommended to use one char/
[in square brackets]
{quote me like that}
In my case I wanted to add quotes only if the string was not already surrounded in quotes, so I did:
(this is slightly different to what I actually did, so it's untested)
public static string SurroundWith(this string text, string ends)
{
if (!(text.StartsWith(ends) && text.EndsWith(ends)))
{
return string.Format("{1}{0}{1}", text, ends);
}
else
{
return text;
}
}
There is no such built in method to do your requirement
There is SplitQuotes method that does something
Input - This is a "very long" string
Output - This, is, a, very long, string
When you get a string from textbox or some control it comes with quotes.
If still you want to place quotes then you can use this kind of method
private string PlaceQuotes(string str, int startPosition, int lastPosition)
{
string quotedString = string.Empty;
string replacedString = str.Replace(str.Substring(0, startPosition),str.Substring(0, startPosition).Insert(startPosition, "'")).Substring(0, lastPosition).Insert(lastPosition, "'");
return String.Concat(replacedString, str.Remove(0, replacedString.Length));
}
Modern C# version below. Using string.Create() we avoid unnecessary allocations:
public static class StringExtensions
{
public static string Quote(this string s) => Surround(s, '"');
public static string Surround(this string s, char c)
{
return string.Create(s.Length + 2, s, (chars, state) =>
{
chars[0] = c;
state.CopyTo(chars.Slice(1));
chars[^1] = c;
});
}
}

Categories