Jump to content

ASP.NET Strings


kwilliams

Recommended Posts

I have what's hopefully a pretty simple question about how to pull parts of a string into variables from within an ASP.NET code-behind doc:. I'm currently researching a site at http://www.aspmatrix.com/asp-net/string_cl...ples/index.aspx to see if I can figure this out on my own, but I thought I'd also check here for some direction. So here is what I have/need:1) Pull the page path:Dim strPagePath As String = Request.ServerVariables("PATH_INFO")2) There are three path options for an ASP.NET file on my site:/maindir/page.aspx/maindir/subdir1/page.aspx/maindir/subdir1/subdir2/page.aspx3) I need to pull up to 4 possible variables (see below). This is where I'm running into a question: How can I pull the following variables from the above path when I don't know whether or not the file will be in the main directory (maindir), sub directory 1 (subdir1), and/or sub directory 2 (subdir2) until after the page is requested by the user?Dim strMainDir As String = "maindir"Dim strSubDir1 As String = "subdir1"Dim strSubDir2 As String = "subdir2"Dim strPageId As String = "page"Thanks for any and all help.

Link to comment
Share on other sites

You could try out the System.IO.FileInfo class to easily get the file name:

FileInfo fi = new FileInfo(strPagePath);strPageId = fi.Name;// OR, if you only want the page name and not the name.extension// strPageId = fi.Name.Replace(fi.Extension, "");

Using that same FileInfo object, you can get the directory information and split it on the '\' character:

string directory = fi.DirectoryName; // C:\maindir\subdir1\subdir2 on my computerstring[] parts = directory.Split(new char[] {'\'} );strMainDir = parts[1];if(parts.length > 2){	strSubDir1 = parts[2];	if(parts.length > 3)	{		strSubDir2 = parts[3];	}}

EDIT: Sorry, I don't know VB so my examples are in C#.

Link to comment
Share on other sites

Dim fi As FileInfo = New FileInfo(strPagePath)strPageId = fi.Name

Dim directory As String = fi.DirectoryNameDim parts As String() = directory.Split("\"C)strMainDir = parts(1)If parts.Length > 2 Then strSubDir1 = parts(2) If parts.Length > 3 Then   strSubDir2 = parts(3) End IfEnd If

I think I got it :) If there are mistakes blame the converter at http://www.developerfusion.co.uk/utilities...csharptovb.aspxBTW it did not like Split(new char[] {'\'} ) even though it is valid code so I changed it Split('\\')BTW Jesh what is the diffrence between the above to ways of splitting?

Link to comment
Share on other sites

Hello all,Before I received your replies, I received a reply yesterday from this forum: http://www.webdeveloper.com/forum/showthre...6352#post706352...and this is what I did with that suggestion:'Declare page pathDim strPagePath As String = Request.ServerVariables("PATH_INFO")Dim myArray = Split(strPagePath, "/") 'the delimiter is the slash'Display the split stringDim iFor i = 0 To UBound(myArray) 'the UBound function returns 3 Response.Write(myArray(i) & "<br>")Next 'move on to the next value of i This solution works, but I'd like to hear your opinions on whether or not it's the best and most-efficient solution when it comes to coding properly in ASP.NET/VB.NET. Thanks for your help and input.

Link to comment
Share on other sites

BTW it did not like Split(new char[] {'\'} ) even though it is valid code so I changed it Split('\\')BTW Jesh what is the diffrence between the above to ways of splitting?
I wasn't aware that you could pass the delimiter as a char rather than a char array. In Visual Studio, when I type ".Sp" after a string variable, intellisense pops up to tell me that Split is a valid method with 6 variations - each of which only accept char[] or string[] rather than char or string. So I've just been passing it as an array. :)The MSDN site says to pass it as an array too:http://msdn2.microsoft.com/en-us/library/s...ring.split.aspxHeck, if Split('\\') works, I'll use it! :)
Link to comment
Share on other sites

I wanted to let you know that I used some of your code and other code to come up with a working solution, and I'd like to post it for any others that may want to use it. So here it is:

Dim strPagePath As String = Request.ServerVariables("PATH_INFO")Dim fi As FileInfo = New FileInfo(strPagePath)'Split page pathDim myArray = Split(strPagePath, "/") 'the delimiter is the slash'Declare array valuesDim i As IntegerFor i = 0 To UBound(myArray) 'the UBound function returns 3		Response.Write(myArray(i) & "<br />") 'testNext 'move on to the next value of i'Assign page path values based on number of directories pulledSelect Case UBound(myArray)		Case 0 'default				Response.Write("There are no directories and the user will be redirected to the home page.") 'test		Case 1				Response.Write("There is one directory.") 'test		Case 2				Response.Write("There are two directories.") 'test		Case 3				Response.Write("There are three directories.") 'testEnd Select'Pull and assign strPageId value from filepath'Dim strPageId = fi.Name 'with extensionDim strPageId = fi.Name.Replace(fi.Extension, "") 'without extension

With this code I'm able to pull the page's full path, split the directories up, perform tasks based on the number of directories in the array, and pull the page name with or without the extension.I don't know if it will help anyone, but it never hurts. Thanks again for your help.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...