473,973 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String help

Hi all,

I have a variable called root which contains
(E:\Web\Website s\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\ima ges\

the result I want should be "images\"

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

blah = path.TrimStart( root.ToCharArra y());

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
16 1612
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.In dexOf(strFind);
String strReturn="";
while(iPos!=-1)
{
strReturn+=strT ext.Substring(0 ,iPos) + strReplace;
strText=strText .Substring(iPos +strFind.Length );
iPos=strText.In dexOf(strFind);
}
if(strText.Leng th>0)
strReturn+=strT ext;
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/
=============== =============== =====
"^MisterJin go^" <mi*********@gm ail.comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.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.Reg ularExpressions ;

string myString;
myString = "This is a test.";
myString = Regex.Replace(m yString, " 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.comwrot e in message
news:ek******** ******@TK2MSFTN GP05.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.In dexOf(strFind);
String strReturn="";
while(iPos!=-1)
{
strReturn+=strT ext.Substring(0 ,iPos) + strReplace;
strText=strText .Substring(iPos +strFind.Length );
iPos=strText.In dexOf(strFind);
}
if(strText.Leng th>0)
strReturn+=strT ext;
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/
=============== =============== =====
"^MisterJin go^" <mi*********@gm ail.comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.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\Websit es\fileBrowse\" ;
string path = @"E:\Web\Websit es\fileBrowse\i mages\";

// You can use Replace this way
path = path.Replace(ro ot, "");
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.In dexOf(strFind);
String strReturn="";
while(iPos!=-1)
{
strReturn+=strT ext.Substring(0 ,iPos) + strReplace;
strText=strText .Substring(iPos +strFind.Length );
iPos=strText.In dexOf(strFind);
}
if(strText.Leng th>0)
strReturn+=strT ext;
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.Reg ularExpressions ;

string myString;
myString = "This is a test.";
myString = Regex.Replace(m yString, " 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.Argument Exception: 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/
=============== =============== =====
"^MisterJin go^" <mi*********@gm ail.comwrote in message
news:11******** **************@ 75g2000cwc.goog legroups.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.Reg ularExpressions ;

string myString;
myString = "This is a test.";
myString = Regex.Replace(m yString, " 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.Argument Exception: parsing "\" - Illegal \
at end of pattern.

Aug 2 '06 #16
In article <ek************ **@TK2MSFTNGP05 .phx.gbl>, Juan T. Llibre
<no***********@ nowhere.comwrit es
>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 = myStringVariabl e.Replace(strin gVar1, 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
2910
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
13035
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'. here is the code that I used to create a table and then add columns to it later, later I populate the rows in the table.
5
2635
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 public Class CSoccerichCommonFunc
6
6143
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 more minutes. 'REMOVE DUBLICATED VALUE FROM ARRAY +++++++++++++++++ Dim col As New Scripting.Dictionary Dim ii As Integer = 0
6
3712
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 system will help me to create new string "ABD" , could somebody can help me how to programe it , thank you very much. I don't know to how to covert string as ASCII and add integer value and covert the integer back to string, hope can support me...
5
471
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 of the string
9
3892
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 case statement like this: sub showHelp(byval frm as String) Select Case (frm) Case "Form1" dim help as new Form1
8
5002
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 string and print it. but i want to use the String.Format() method if possible to do it.
13
3191
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 creates a character string containing the first n Fibonacci numbers - F(n) = F(n - 1) + F(n - 2), F(0) = F(1) = 1 -, separated by comma. n should be given as an argument to the program. The recursive function should only take one parameter, n, and...
37
1899
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 --srcport 131.188.37.59 --destaddress 1398 --destport tcp --protocol
0
11807
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11557
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10901
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
10070
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8452
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6405
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6542
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4726
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.