473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

C# .NET TrimStart() usage

20
Hello,
I'm a real rookie when it comes to .NET and I'm having some trouble. I'm looking to trim the beginning characters from a server variable that I'm getting from session state. I'm populating the session state with "Auth_User" and am looking to trim "domain/" from "domain/user" and return just the user name when I use <%Session=["User"]%>. Is this even possible or am I peeing in the wind? Thanks in advance!
Aug 16 '07 #1
10 7056
Plater
7,872 Expert 4TB
If it's a string, you could try doing a search for the "/" character and getting the character position ( mystring.IndexOf("/") ) and then doing a substring of it.

So for example:
Expand|Select|Wrap|Line Numbers
  1. string mys="mydomain/plater";
  2. int n=mys.IndexOf("/");
  3. if (n!=-1)//if "/" is not in the string a -1 is returned
  4. {
  5.    mys=mys.Substring(n+1);//also want to remove the "/" character
  6. }
  7. //now the varriable mys should contain "plater"
  8.  
Aug 16 '07 #2
stilmas
20
If it's a string, you could try doing a search for the "/" character and getting the character position ( mystring.IndexOf("/") ) and then doing a substring of it.

So for example:
Expand|Select|Wrap|Line Numbers
  1. string mys="mydomain/plater";
  2. int n=mys.IndexOf("/");
  3. if (n!=-1)//if "/" is not in the string a -1 is returned
  4. {
  5.    mys=mys.Substring(n+1);//also want to remove the "/" character
  6. }
  7. //now the varriable mys should contain "plater"
  8.  

This looks interesting..Is it possible you could show me how this would be coded on a page to use this with the AUTH_USER server variable and session state? Thanks!
Here's my code that I'm experimenting with:

<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<%Session["User"] = Request.ServerVariables["AUTH_USER"]; %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
</head>
<body>
<p><%=Session["User"]%></p>
</body>
</html>
Aug 16 '07 #3
Plater
7,872 Expert 4TB
I think this will work
Expand|Select|Wrap|Line Numbers
  1.  
  2. <p>
  3. <%
  4. string mys=Session["User"];
  5. int n=mys.IndexOf("/");
  6. if (n!=-1)//if "/" is not in the string a -1 is returned
  7. {
  8.    mys=mys.Substring(n+1);//also want to remove the "/" character
  9. }
  10. %>
  11. <%=mys %>
  12. </p>
  13.  
Aug 16 '07 #4
stilmas
20
I think this will work
Expand|Select|Wrap|Line Numbers
  1.  
  2. <p>
  3. <%
  4. string mys=Session["User"];
  5. int n=mys.IndexOf("/");
  6. if (n!=-1)//if "/" is not in the string a -1 is returned
  7. {
  8.    mys=mys.Substring(n+1);//also want to remove the "/" character
  9. }
  10. %>
  11. <%=mys %>
  12. </p>
  13.  
Getting close! I really appreciate the help here. Here's the error I get:
Compiler Error Message: CS0266: Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)

Source Error:



Line 2: <%Session["User"] = Request.ServerVariables["AUTH_USER"]; %>
Line 3: <%
Line 4: string mys=Session["User"];
Line 5: int n=mys.IndexOf("/");
Line 6: if (n!=-1)//if "/" is not in the string a -1 is returned
Aug 16 '07 #5
Plater
7,872 Expert 4TB
Ooops

string mys=Session["User"].ToString();
Aug 16 '07 #6
stilmas
20
Ooops

string mys=Session["User"].ToString();

No problem.. I have an OOOPS! too. the slash is actually a backslash "\" and not a forward slash "/". That's my fault. I think that changes things, it seems like it doesn't like the backslash.
Aug 16 '07 #7
stilmas
20
Ooops

string mys=Session["User"].ToString();

From what I just read on Google that C# thinks "\" is a switch. Other people looking to use the "\" also. I'll keep looking around unless you know how to get around that. Thanks a million!
Aug 16 '07 #8
Plater
7,872 Expert 4TB
Well you said "/" which is not a special character
So I gave:
int n=mys.IndexOf("/");

But if you want "\"
int n=mys.IndexOf("\\");
or
int n=mys.IndexOf(@"\");
Aug 16 '07 #9
stilmas
20
Ooops

string mys=Session["User"].ToString();

Dude,

I found it. use"\\" for a single backslash. It all works nicely! Thanks so much for your help! Now my hair can regrow!
Aug 16 '07 #10
stilmas
20
Well you said "/" which is not a special character
So I gave:
int n=mys.IndexOf("/");

But if you want "\"
int n=mys.IndexOf("\\");
or
int n=mys.IndexOf(@"\");
Yes, that was my fault. I typo'd it. Yes you are correct, \\ works fine! Thanks so much!
Aug 16 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: rbt | last post by:
Would a Python process consume more memory on a PC with lots of memory? For example, say I have the same Python script running on two WinXP computers that both have Python 2.4.0. One computer has...
2
by: tomvr | last post by:
Hello I have noticed some 'weird' memory usage in a vb.net windows app The situation is as follows I have an app (heavy on images) with 2 forms (actually there are more forms and on starting...
3
by: Ian Taite | last post by:
Hello, I'm exploring why one of my C# .NET apps has "high" memory usage, and whether I can reduce the memory usage. I have an app that wakes up and processes text files into a database...
26
by: Bruno Jouhier [MVP] | last post by:
I'm currently experiencing a strange phenomenon: At my Office, Visual Studio takes a very long time to compile our solution (more than 1 minute for the first project). At home, Visual Studio...
7
by: GrandpaB | last post by:
I am comparing two strings for sorting. In some cases the string may be enclosed in quotes. Since the quote character is less than the A character, all the strings enclosed in quotes will finish...
2
by: brian_harris | last post by:
I am tring to use trimstart to remove leading zeros, but all things I try give a compiler error on converting data. I am programing in C++ .net vs2003. This is one of my earlier attempts to call...
10
by: rdemyan via AccessMonster.com | last post by:
My app contains utility meter usage. One of the things we have to deal with is when a usage is clearly incorrect. Perhaps someone wrote the meter reading down incorrectly or made a factor of 10...
3
by: Sirisha | last post by:
I am using the following code to get the CPU usage PerformanceCounter myCounter; myCounter = new PerformanceCounter(); myCounter.CategoryName = "Processor"; myCounter.CounterName = "%...
2
by: jld | last post by:
Hi, I developed an asp.net based eCommerce Website for a client and it is hosted at discount asp. The site is quite interactive, queries a database a lot and uses ajax.asp.net to spice up...
2
by: sjoshi | last post by:
Is List.ConvertAll the only way to apply TrimStart to elements of a List<stringor is there a better way ? Currently I'm doing this... List<stringlst = new List<string>(); lst.AddRange(new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.