Gerico
Gerico

Reputation: 5179

VB.net .replace start and end of string

I use this to dynamically place the system variable of URL as the class on the body. The problem is that it strips the first trailing '/' and replaces it with a hyphen, which is annoying.

How could I prevent this and just replace the first '/' with nothing?

VB

<body class="<%=Request.ServerVariables("URL").Replace(".aspx","").Replace("/","-")%>">

At the moment if I use:

<body class="<%=Request.ServerVariables("URL").Replace(".aspx","").Replace("/","")%>">

I get something like 'userprofileedit' from a URL of /user/profile/edit

What I actually want is 'user-profile-edit' as a class on my body instead of 'userprofileedit'. My first example:

<body class="<%=Request.ServerVariables("URL").Replace(".aspx","").Replace("/","-")%>">

Does what I need, however I then get a starting hyphen due to the first / from '/user..' - hope this better explains my problem.

Upvotes: 0

Views: 1248

Answers (2)

Tony Hinkle
Tony Hinkle

Reputation: 4742

Use the Remove function to strip off the first character:

<body class="<%=Request.ServerVariables("URL").Replace(".aspx", "").Replace("/", "-").Remove(0, 1)

Upvotes: 0

Guffa
Guffa

Reputation: 700730

You can use Substring(1) to get everything but the first character:

<body class="<%=Request.ServerVariables("URL").Substring(1).Replace(".aspx","").Replace("/","-")%>">

Note: The approach of using Replace to remove the .aspx from the page would also remove it from a folder if it would happen to contain that, but as long as you are aware of that and don't name the folders that way, you're safe.

Upvotes: 2

Related Questions