ASP string manipulation

User avatar
Jezza
5StarLounger
Posts: 847
Joined: 24 Jan 2010, 06:35
Location: A Magic Forest in Deepest, Darkest, Kent

ASP string manipulation

Post by Jezza »

I am creating a classic ASP that will output form inputs to an XML file and one requirement for auditing purposes is to have the NT user name added, I am using the code below to obtain that but it comes out in the form of myDomain\JBear however I would like it to come out as JBear.

Code: Select all

{
	// Function to get user name currently logged into Workbench so their details can
	//be added to the XML output
  
	var strUser =Request.ServerVariables("LOGON_USER");
	
}
I have tried string manipulation but all I get is as error shown below where line 25 is the location of the string function:

Code: Select all

Microsoft JScript runtime error '800a138f' 

Object expected 

/workbook/workbench.asp, line 25 
I would have added something like:

Code: Select all

var strUser=substring(strUser,9);
Any ideas?
Jerry
I’ll be more enthusiastic about encouraging thinking outside the box when there’s evidence of any thinking going on inside it

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: ASP string manipulation

Post by HansV »

Try this:

Code: Select all

{
   // Function to get user name currently logged into Workbench so their details can
   //be added to the XML output
  
   var strUser=Request.ServerVariables('LOGON_USER');
   var p=strUser.indexOf('\');
   strUser=strUser.substr(p+1);

}
In this example, substr and substring have the same result: if you omit the second argument, the entire substring starting at the position specified by the first argument is returned.
Best wishes,
Hans

User avatar
Jezza
5StarLounger
Posts: 847
Joined: 24 Jan 2010, 06:35
Location: A Magic Forest in Deepest, Darkest, Kent

Re: ASP string manipulation

Post by Jezza »

Thanks Hans, that makes sense but now I get:

Code: Select all

Microsoft JScript compilation error '800a03f7' 

Unterminated string constant 

/workbook/Workbench.asp, line 25 

var p=strUser.indexOf("\");
---------------------------^
So I changed it to

Code: Select all

var strUser =Request.ServerVariables("LOGON_USER");
	//var p=strUser.indexOf('\');
    strUser=strUser.substring(11);
and now get:

Code: Select all

Microsoft JScript runtime error '800a01b6' 

Object doesn't support this property or method 

/workbook/Workbench.asp, line 26 
Weird :hairout:
Jerry
I’ll be more enthusiastic about encouraging thinking outside the box when there’s evidence of any thinking going on inside it

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: ASP string manipulation

Post by HansV »

He he - \ is a special character in strings, so you have to escape it with another \. So try

Code: Select all

{
   // Function to get user name currently logged into Workbench so their details can
   //be added to the XML output
  
   var strUser=Request.ServerVariables('LOGON_USER');
   var p=strUser.indexOf('\\');
   strUser=strUser.substr(p+1);

}
Best wishes,
Hans

User avatar
Jezza
5StarLounger
Posts: 847
Joined: 24 Jan 2010, 06:35
Location: A Magic Forest in Deepest, Darkest, Kent

Re: ASP string manipulation

Post by Jezza »

:-) I tried that in the interim period but I get

Object doesn't support this property or method

Interesting enough though as soon as I change strUser to "1234\ABCD" it does allow it but returns 1234ABCD so it must be the way it sees Request.ServerVariables("LOGON_USER")
Jerry
I’ll be more enthusiastic about encouraging thinking outside the box when there’s evidence of any thinking going on inside it

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: ASP string manipulation

Post by HansV »

I'm afraid I'm out of my depth here. Sorry!
Best wishes,
Hans

User avatar
Jezza
5StarLounger
Posts: 847
Joined: 24 Jan 2010, 06:35
Location: A Magic Forest in Deepest, Darkest, Kent

Re: ASP string manipulation

Post by Jezza »

Deleted content by Jezza as I got a bit clumsy and reposted in corect format!!!
Last edited by Jezza on 07 Jul 2011, 14:20, edited 1 time in total.
Jerry
I’ll be more enthusiastic about encouraging thinking outside the box when there’s evidence of any thinking going on inside it

User avatar
Jezza
5StarLounger
Posts: 847
Joined: 24 Jan 2010, 06:35
Location: A Magic Forest in Deepest, Darkest, Kent

Re: ASP string manipulation

Post by Jezza »

No problems at all, thanks for your help

Just to say where I have got so far:

If I use:

Code: Select all

var strUser = "myDomain\JBear"; //Request.ServerVariables("LOGON_USER"); 
var p=strUser.indexOf('\\');
strUser=strUser.substr(p+1);
It returns JBear

It just does not seem to like Request.ServerVariables("LOGON_USER"); becuase if you write the value p it returns -1 which is where it fails..maddening
Jerry
I’ll be more enthusiastic about encouraging thinking outside the box when there’s evidence of any thinking going on inside it

User avatar
Jezza
5StarLounger
Posts: 847
Joined: 24 Jan 2010, 06:35
Location: A Magic Forest in Deepest, Darkest, Kent

Re: ASP string manipulation

Post by Jezza »

....and just to put closure on it, I forced the Request.ServerVariables("LOGON_USER"); to be treated as a string by adding _space_ to the end and it works fine :grin:

Code: Select all

// Function to get user name currently logged into Workbench so their details can
	//be added to the XML output
	
	var strLogOn= Request.ServerVariables("LOGON_USER");
	
	strUser = strLogOn + " ";
	
	var p=strUser.indexOf('\\');
    strUser=strUser.substr(p+1);
	//Response.Write(strUser);
Jerry
I’ll be more enthusiastic about encouraging thinking outside the box when there’s evidence of any thinking going on inside it

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: ASP string manipulation

Post by HansV »

Ah - so Request.ServerVariables("LOGON_USER") is probably an object, and by adding a space you force JavaScript to use its default property - the name...
Best wishes,
Hans