468,766 Members | 1,300 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,766 developers. It's quick & easy.

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 1388
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Helmut Jarausch | last post: by
5 posts views Thread by Sia Jai Sung | last post: by
6 posts views Thread by Calros Lo | last post: by
5 posts views Thread by Joe Nova | last post: by
8 posts views Thread by Lucky | last post: by
37 posts views Thread by xyz | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.