473,411 Members | 1,918 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,411 software developers and data experts.

String Trimming with IndexOf C#

20
Hey guys,

I have a quick question that should be easy for the people here. If this code trims the string to give me the first half of the string (anything before the \):
<%
string mys=Request.ServerVariables["AUTH_USER"].ToString();
int n=mys.IndexOf("\\");
if (n!=-1)
{
mys=mys.Substring(n+1);
}
%>

How would I wirte it to give me the second half only? (anything after the \).
Thanks a million!
Aug 17 '07 #1
2 2391
Plater
7,872 Expert 4TB
That code is giving you the second half.
If you want the first half:
mys=mys.Substring(0,n-1);

OR you could do
Expand|Select|Wrap|Line Numbers
  1. if (mys.indexOf("\\")!=0)
  2. {
  3.    string[] pieces=mys.Split("\\");
  4.    //pieces[0] is anything before the first \
  5.    //pieces[1] will be what comes after
  6.    // (Note: if there are more \ in the string then there will be a pieces[2] and so on
  7. }
  8.  
Aug 17 '07 #2
stilmas
20
That code is giving you the second half.
If you want the first half:
mys=mys.Substring(0,n-1);

OR you could do
Expand|Select|Wrap|Line Numbers
  1. if (mys.indexOf("\\")!=0)
  2. {
  3.    string[] pieces=mys.Split("\\");
  4.    //pieces[0] is anything before the first \
  5.    //pieces[1] will be what comes after
  6.    // (Note: if there are more \ in the string then there will be a pieces[2] and so on
  7. }
  8.  
Hey Plater,

I'm getting burnt. That's what I meant. Thanks for all your help.
Aug 17 '07 #3

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

Similar topics

8
by: Phil Powell | last post by:
if (document.location.href.indexOf('?') >= 0) document.location.href = document.location.href.substring(0, document.location.href.indexOf('?')); if (document.location.href.indexOf('#') >= 0) {...
2
by: José Joye | last post by:
Hello, I was wondering if there is a method that exists to replace multi-spaces within a string with single-space. eg: "12 3 4 56" --> "12 3 4 56" I think this could be done by...
4
by: Jason Gleason | last post by:
What's the most efficient way to get the number of occurences of a certain string in another string..for instance i'm using the following code right now... private int CharacterCounter(String...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
35
by: Cor | last post by:
Hallo, I have promised Jay B yesterday to do some tests. The subject was a string evaluation that Jon had send in. Jay B was in doubt what was better because there was a discussion in the C#...
2
by: Badass Scotsman | last post by:
Hello, Using VB and ASP,NET I would like to be able to search a STRING for a smaller STRING within, based on the characters which appear before and after. For example: String1 = " That was...
2
by: richardkreidl | last post by:
I have the following string below in which I need to pull out the name that follows: ContactsManager:" . In this particular case it would be: BARBARA JOSEPH Job Completion, Failure...
1
by: Lilith | last post by:
I'm looking at practical string lengths and the capacity of a text box. I'm using a string to progressively add the status of a process periodically and dumping that text into a text box. I don't...
8
by: SMJT | last post by:
Does anyone know why the string contains function always returns true if the token is an empty string? I expected it to return false. "AnyOldText".Contains("") or...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.