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

String help

Hi all,

I have a variable called root which contains
(E:\Web\Websites\fileBrowse\) and then I have a variable called path
which uses root, and adds directories on to it (this is part of a file
browser i've built). I'm trying to simply remove the root string from
the path string, so for example below:

root = E:\Web\Websites\fileBrowse\
path = E:\Web\Websites\fileBrowse\images\

the result I want should be "images\"

I'm attempting to do this using the following line of code:

blah = path.TrimStart(root.ToCharArray());

This isn't working though. Instead of getting "images\", i'm getting
"ages\". I've tried trimming using a start and end element of the char
array but I still get the same result. It seems that for some reason
the "\" which is trimmed at the end of root is taken as a control
character and so takes the proceding two characters too "im".

Has anyone got any idea how to stop this happening? I'm hoping it's
something simple which i'm overlooking.

Any help would be appreciated.

Aug 2 '06 #1
16 1564
Dim root As String = "E:\Web\Websites\fileBrowse\"
Dim path As String = "E:\Web\Websites\fileBrowse\images\"

MessageBox.Show(Replace(path, root, ""))

- Augustin

"^MisterJingo^" wrote:
Hi all,

I have a variable called root which contains
(E:\Web\Websites\fileBrowse\) and then I have a variable called path
which uses root, and adds directories on to it (this is part of a file
browser i've built). I'm trying to simply remove the root string from
the path string, so for example below:

root = E:\Web\Websites\fileBrowse\
path = E:\Web\Websites\fileBrowse\images\

the result I want should be "images\"

I'm attempting to do this using the following line of code:

blah = path.TrimStart(root.ToCharArray());

This isn't working though. Instead of getting "images\", i'm getting
"ages\". I've tried trimming using a start and end element of the char
array but I still get the same result. It seems that for some reason
the "\" which is trimmed at the end of root is taken as a control
character and so takes the proceding two characters too "im".

Has anyone got any idea how to stop this happening? I'm hoping it's
something simple which i'm overlooking.

Any help would be appreciated.

Aug 2 '06 #2
Interesting..

Without looking much into it, you might be better off simply doing:

path.Substring(root.Length); (which I tested and it works).

I'll need to look more into why TrimStart() is behaving that way..

Karl

--
http://www.openmymind.net/
http://www.codebetter.com/
"^MisterJingo^" <mi*********@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Hi all,

I have a variable called root which contains
(E:\Web\Websites\fileBrowse\) and then I have a variable called path
which uses root, and adds directories on to it (this is part of a file
browser i've built). I'm trying to simply remove the root string from
the path string, so for example below:

root = E:\Web\Websites\fileBrowse\
path = E:\Web\Websites\fileBrowse\images\

the result I want should be "images\"

I'm attempting to do this using the following line of code:

blah = path.TrimStart(root.ToCharArray());

This isn't working though. Instead of getting "images\", i'm getting
"ages\". I've tried trimming using a start and end element of the char
array but I still get the same result. It seems that for some reason
the "\" which is trimmed at the end of root is taken as a control
character and so takes the proceding two characters too "im".

Has anyone got any idea how to stop this happening? I'm hoping it's
something simple which i'm overlooking.

Any help would be appreciated.

Aug 2 '06 #3
Augustin Prasanna wrote:
Dim root As String = "E:\Web\Websites\fileBrowse\"
Dim path As String = "E:\Web\Websites\fileBrowse\images\"

MessageBox.Show(Replace(path, root, ""))

- Augustin
Hi Augustin,

I'm not very up on VB, so could you tell me what the Replace is a
method of? I can find string.Replace. But that only takes 2 single
chars. Also MessageBoax isn't availible to ASP.Net pages is it?
Sorry if i'm being dense here.

Aug 2 '06 #4
Oh..I got it :)

TrimStart removes all references to any array of the characters you pass in.

So if you did:

"abcd".TrimStart('d','c','b','a');

it would remove everything - ie, they don't need to be in order.

You'll notice that the 'i' is being trimmed, but that's because you're
passing it an 'i' as a character to trim. It keeps going, removing ANY
character you passed in (in any order) until it finds one that wasn't passed
in and stops.

Karl

--
http://www.openmymind.net/
http://www.codebetter.com/
"^MisterJingo^" <mi*********@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Hi all,

I have a variable called root which contains
(E:\Web\Websites\fileBrowse\) and then I have a variable called path
which uses root, and adds directories on to it (this is part of a file
browser i've built). I'm trying to simply remove the root string from
the path string, so for example below:

root = E:\Web\Websites\fileBrowse\
path = E:\Web\Websites\fileBrowse\images\

the result I want should be "images\"

I'm attempting to do this using the following line of code:

blah = path.TrimStart(root.ToCharArray());

This isn't working though. Instead of getting "images\", i'm getting
"ages\". I've tried trimming using a start and end element of the char
array but I still get the same result. It seems that for some reason
the "\" which is trimmed at the end of root is taken as a control
character and so takes the proceding two characters too "im".

Has anyone got any idea how to stop this happening? I'm hoping it's
something simple which i'm overlooking.

Any help would be appreciated.

Aug 2 '06 #5
Karl Seguin [MVP] wrote:
Interesting..

Without looking much into it, you might be better off simply doing:

path.Substring(root.Length); (which I tested and it works).

I'll need to look more into why TrimStart() is behaving that way..

Karl

Hi Karl,

That works perfectly, thanks. I've no idea why TrimStart is behaving
that way :\.

Aug 2 '06 #6
Karl Seguin [MVP] wrote:
Oh..I got it :)

TrimStart removes all references to any array of the characters you pass in.

So if you did:

"abcd".TrimStart('d','c','b','a');

it would remove everything - ie, they don't need to be in order.

You'll notice that the 'i' is being trimmed, but that's because you're
passing it an 'i' as a character to trim. It keeps going, removing ANY
character you passed in (in any order) until it finds one that wasn't passed
in and stops.

Karl

--
http://www.openmymind.net/
http://www.codebetter.com/
Ah, I didn't know this :). I thought it just trimmed them in the order
passed, up until the last element of the char array.

Aug 2 '06 #7
Replace function will accept 3 parameters.

Replace (expression, find, replace)

for e.g replace ('test', 's', 'e') will return 'teet' because 's' will be
replaced with 'e'.

I hope this helps you.

MessageBox is for testing purpose. it can be used in windows app. you can
try 'Response.Write' to test the output

"^MisterJingo^" wrote:
Hi Augustin,

I'm not very up on VB, so could you tell me what the Replace is a
method of? I can find string.Replace. But that only takes 2 single
chars. Also MessageBoax isn't availible to ASP.Net pages is it?
Sorry if i'm being dense here.

Aug 2 '06 #8
Augustin Prasanna wrote:
Replace function will accept 3 parameters.

Replace (expression, find, replace)

for e.g replace ('test', 's', 'e') will return 'teet' because 's' will be
replaced with 'e'.

I hope this helps you.

MessageBox is for testing purpose. it can be used in windows app. you can
try 'Response.Write' to test the output
Is the Replace function VB only? I can't seem to find a standalone
replace function in C#.

Aug 2 '06 #9
The replace function is available on the string variable you have.
Here's a short sample. Hope this helps.

string root = @"E:\Web\Websites\fileBrowse\";
string path = @"E:\Web\Websites\fileBrowse\images\";

// You can use Replace this way
path = path.Replace(root, "");
Response.Write(path);

^MisterJingo^ wrote:
Augustin Prasanna wrote:
Replace function will accept 3 parameters.

Replace (expression, find, replace)

for e.g replace ('test', 's', 'e') will return 'teet' because 's' will be
replaced with 'e'.

I hope this helps you.

MessageBox is for testing purpose. it can be used in windows app. you can
try 'Response.Write' to test the output

Is the Replace function VB only? I can't seem to find a standalone
replace function in C#.
Aug 2 '06 #10
re:
Is the Replace function VB only?
Yes.

re:
I can't seem to find a standalone replace function in C#.
There isn't one.

Here's a custom Replace function I found some time ago :

public static String Replace(String strText,String strFind,String strReplace)
{
int iPos=strText.IndexOf(strFind);
String strReturn="";
while(iPos!=-1)
{
strReturn+=strText.Substring(0,iPos) + strReplace;
strText=strText.Substring(iPos+strFind.Length);
iPos=strText.IndexOf(strFind);
}
if(strText.Length>0)
strReturn+=strText;
return strReturn;
}
}

....it accepts the same 3 parameters as the VB Replace function.


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"^MisterJingo^" <mi*********@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
Augustin Prasanna wrote:
>Replace function will accept 3 parameters.

Replace (expression, find, replace)

for e.g replace ('test', 's', 'e') will return 'teet' because 's' will be
replaced with 'e'.

I hope this helps you.

MessageBox is for testing purpose. it can be used in windows app. you can
try 'Response.Write' to test the output

Is the Replace function VB only? I can't seem to find a standalone
replace function in C#.

Aug 2 '06 #11
I should have added that you can also use Regex to replace characters in a string :

using System;
using System.Text.RegularExpressions;

string myString;
myString = "This is a test.";
myString = Regex.Replace(myString, " is", " was");


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:ek**************@TK2MSFTNGP05.phx.gbl...
re:
>Is the Replace function VB only?

Yes.

re:
>I can't seem to find a standalone replace function in C#.

There isn't one.

Here's a custom Replace function I found some time ago :

public static String Replace(String strText,String strFind,String strReplace)
{
int iPos=strText.IndexOf(strFind);
String strReturn="";
while(iPos!=-1)
{
strReturn+=strText.Substring(0,iPos) + strReplace;
strText=strText.Substring(iPos+strFind.Length);
iPos=strText.IndexOf(strFind);
}
if(strText.Length>0)
strReturn+=strText;
return strReturn;
}
}

...it accepts the same 3 parameters as the VB Replace function.


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"^MisterJingo^" <mi*********@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
>Augustin Prasanna wrote:
>>Replace function will accept 3 parameters.

Replace (expression, find, replace)

for e.g replace ('test', 's', 'e') will return 'teet' because 's' will be
replaced with 'e'.

I hope this helps you.

MessageBox is for testing purpose. it can be used in windows app. you can
try 'Response.Write' to test the output

Is the Replace function VB only? I can't seem to find a standalone
replace function in C#.


Aug 2 '06 #12
Gozirra wrote:
The replace function is available on the string variable you have.
Here's a short sample. Hope this helps.

string root = @"E:\Web\Websites\fileBrowse\";
string path = @"E:\Web\Websites\fileBrowse\images\";

// You can use Replace this way
path = path.Replace(root, "");
Response.Write(path);

I found the string one :). I was getting confused as that accepts only
two parameters and the one mentioned above accepts three. So I was
curious if it was a VB only thing :).

Aug 2 '06 #13

Juan T. Llibre wrote:
re:
Is the Replace function VB only?

Yes.

re:
I can't seem to find a standalone replace function in C#.

There isn't one.

Here's a custom Replace function I found some time ago :

public static String Replace(String strText,String strFind,String strReplace)
{
int iPos=strText.IndexOf(strFind);
String strReturn="";
while(iPos!=-1)
{
strReturn+=strText.Substring(0,iPos) + strReplace;
strText=strText.Substring(iPos+strFind.Length);
iPos=strText.IndexOf(strFind);
}
if(strText.Length>0)
strReturn+=strText;
return strReturn;
}
}

...it accepts the same 3 parameters as the VB Replace function.

Thanks for that Juan :).

Aug 2 '06 #14

Juan T. Llibre wrote:
I should have added that you can also use Regex to replace characters in a string :

using System;
using System.Text.RegularExpressions;

string myString;
myString = "This is a test.";
myString = Regex.Replace(myString, " is", " was");
I've been trying to use this to replace '\' with '/', but it doesnt
seem to like it as I get the error:

Exception Details: System.ArgumentException: parsing "\" - Illegal \
at end of pattern.

Aug 2 '06 #15
Have you tried using a literal string... :
http://www.peachpit.com/articles/art...seqNum=10&rl=1

or an escape character... :
http://www.peachpit.com/articles/art...&seqNum=9&rl=1

?

That article is a goldmine of info for string manipulation in C#, btw.


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"^MisterJingo^" <mi*********@gmail.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
>
Juan T. Llibre wrote:
>I should have added that you can also use Regex to replace characters in a string :

using System;
using System.Text.RegularExpressions;

string myString;
myString = "This is a test.";
myString = Regex.Replace(myString, " is", " was");

I've been trying to use this to replace '\' with '/', but it doesnt
seem to like it as I get the error:

Exception Details: System.ArgumentException: parsing "\" - Illegal \
at end of pattern.

Aug 2 '06 #16
In article <ek**************@TK2MSFTNGP05.phx.gbl>, Juan T. Llibre
<no***********@nowhere.comwrites
>Is the Replace function VB only?

Yes.
That's a rather misleading reply isn't it? There's a replace function in
C#, it just isn't stand-alone, it is a method of the string itself.
>re:
>I can't seem to find a standalone replace function in C#.

There isn't one.
So use...

string result = myStringVariable.Replace(stringVar1, stringVar2);

--
Alan Silver
(anything added below this line is nothing to do with me)
Aug 8 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Helmut Jarausch | last post by:
Hi, entering help('rstrip') or help('ljust') into IDLE's shell window I only get no Python documentation found ...
3
by: Imran Aziz | last post by:
Hello All, I am getting the following error on our production server, and I dont get the same error on the development box. Unable to cast object of type 'System.Byte' to type 'System.String'. ...
5
by: Sia Jai Sung | last post by:
Hi, I have a class that I modify from a sample program, like below ========================================== Imports System Imports System.Web.UI Imports System.Security.Cryptography ...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
6
by: Calros Lo | last post by:
Dear all: I develop a programe that need when I get a string , such as "123" or "ABC",if I get string "123" and the system will help me to create new string "124" , if I get string "ABC" and the...
5
by: Joe Nova | last post by:
I'm a C++ noob and I need a little help manipulating strings. I've got a program that takes an expression in the form: "operand1 operator operand2" I'd like to: 1. Find the total length...
9
by: MikeB | last post by:
Hi, I'd appreciate some help, please. I'm writing a VS2005 VB project for school and one of the requirements is that every screen should have a "Help" button. I could do it by writing a clumsy...
8
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to...
13
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that...
37
by: xyz | last post by:
I have a string 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168 for example lets say for the above string 16:23:18.659343 -- time 131.188.37.230 -- srcaddress 22 ...
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
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
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,...

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.