how to find characters position and split string in asp.net

Posted by Gow.Net under ASP.NET on 9/20/2012 | Points: 10 | Views : 7372 | Status : [Member] | Replies : 3
hi
i am using file upload control in asp.net when upload my file i get file path using System .IO

string filename="c:\Program Files\Microsoft Visual Studio 9.0\Common7\ADMIN\Loginpage.aspx "


here how to split above string in

    string strname="ADMIN\Loginpage.aspx "



any one give some idea for Do this

gowthaman8870226416


Responses

Posted by: Kotra.Ramakrishna on: 9/20/2012 [Member] Starter | Points: 25

Up
0
Down
Hi, Please try below code.

string filename = @"c:\Program Files\Microsoft Visual Studio 9.0\Common7\ADMIN\Loginpage.aspx ";

if (!string.IsNullOrEmpty(filename))
{
string secondPart = string.Empty;
string firstPart=filename.Substring(0,filename.LastIndexOf(@"\"));
if(!string.IsNullOrEmpty(firstPart))
{
secondPart = firstPart.Substring(firstPart.LastIndexOf(@"\")+1);
firstPart = filename.Substring(filename.LastIndexOf(@"\") );
}
if (!string.IsNullOrEmpty(firstPart) && !string.IsNullOrEmpty(secondPart))
{
string path = secondPart+firstPart;
}
}

Thanks & Regards
Ramakrishna.k

Gow.Net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vikash on: 9/21/2012 [Member] Starter | Points: 25

Up
0
Down
You can use this simle code.....

string filename = @"c:\Program Files\Microsoft Visual Studio 9.0\Common7\ADMIN\Loginpage.aspx ";
string[] str = filename.Split('\\');

string newstring = str[4].ToString() + @"\" + str[5].ToString();



Regards,
Vikash Pathak

Gow.Net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pgayath on: 9/21/2012 [Member] Starter | Points: 25

Up
0
Down
Gowthaman:

You can use the below code for whatever filename may be, it will display the last two parts.

string strFileName = @"c:\Program Files\Microsoft Visual Studio 9.0\Common7\ADMIN\Loginpage.aspx ";

string[] arrstrFileName = null;
string strOutput = null;

arrstrFileName = strFileName.Split('\\');

if ((arrstrFileName != null) && (arrstrFileName.Length > 0))
{
strOutput = arrstrFileName[arrstrFileName.Length - 2].ToString() + @"\" + arrstrFileName[arrstrFileName.Length - 1].ToString();
}

Response.write(strOutput);


Thanks and Regards,
Gayathri P

Gow.Net, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response