String help

M

^MisterJingo^

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.
 
G

Guest

Dim root As String = "E:\Web\Websites\fileBrowse\"
Dim path As String = "E:\Web\Websites\fileBrowse\images\"

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

- Augustin
 
K

Karl Seguin [MVP]

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
 
K

Karl Seguin [MVP]

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
 
M

^MisterJingo^

Augustin said:
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.
 
M

^MisterJingo^

Karl said:
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 :\.
 
M

^MisterJingo^

Karl said:
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

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.
 
G

Guest

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
 
M

^MisterJingo^

Augustin said:
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#.
 
G

Gozirra

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);
 
J

Juan T. Llibre

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.
 
J

Juan T. Llibre

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");
 
M

^MisterJingo^

Gozirra said:
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 :).
 
M

^MisterJingo^

Juan said:
re:

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 :).
 
M

^MisterJingo^

Juan said:
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.
 
A

Alan Silver

Juan T. Llibre said:

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:

There isn't one.

So use...

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,097
Latest member
RayE496148

Latest Threads

Top