473,503 Members | 2,105 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best Way to Replace string character

I have the following letters;

string letters = "a;b;c....to z";

the I need to replace the incoming string which containing letters above
with integer 1 i did following

for(int u=0;u<letters.Split(';').Length;u++) {
FilesName = FilesName.Trim().Replace(letters.Split(';')[u], "1");
}

is there better way to do that
Nov 19 '05 #1
3 2676
Well,
You could use a regular expression. Failing that, you could write your code
to be a lot more efficient.

StringBuilder sb = new StringBuilder(letters.Length);
char[] letters = FilesName.Trim().ToCharArray();
for each (char letter in letters){
sb.Append( letter == ';' ? ';' : '1' ) ;
}
if (letter != ';'){
sb.Append("1");
}
}

or something similar...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Raed Sawalha" <Ra*********@discussions.microsoft.com> wrote in message
news:98**********************************@microsof t.com...
I have the following letters;

string letters = "a;b;c....to z";

the I need to replace the incoming string which containing letters above
with integer 1 i did following

for(int u=0;u<letters.Split(';').Length;u++) {
FilesName = FilesName.Trim().Replace(letters.Split(';')[u], "1");
}

is there better way to do that

Nov 19 '05 #2
> is there better way to do that

Sure is. You only need to split the string once, so create a string array
variable, and use that array in your loop. It prevents the recurring split,
which will consume a lot of resources creating arrays of strings that are
constantly being thrown away.

In fact, assumming that you want to use the lettters of the English alphabet
(or any alphabet, for that matter), as you do in your example, you don't
need an array of strings at all. You can create each letter in the alphabet
using its numerical value. As each letter has a numerical value that is
sequential, you can apply math to a single numerical value to get each
letter. This can be done by casting an integer to a char.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.

"Raed Sawalha" <Ra*********@discussions.microsoft.com> wrote in message
news:98**********************************@microsof t.com...
I have the following letters;

string letters = "a;b;c....to z";

the I need to replace the incoming string which containing letters above
with integer 1 i did following

for(int u=0;u<letters.Split(';').Length;u++) {
FilesName = FilesName.Trim().Replace(letters.Split(';')[u], "1");
}

is there better way to do that

Nov 19 '05 #3
egads..just got to work and saw that i had some leftovers in there...should
be:

StringBuilder sb = new StringBuilder(letters.Length);
char[] letters = FilesName.Trim().ToCharArray();
for each (char letter in letters){
sb.Append( letter == ';' ? ';' : '1' ) ;
}
Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl...
Well,
You could use a regular expression. Failing that, you could write your
code to be a lot more efficient.

StringBuilder sb = new StringBuilder(letters.Length);
char[] letters = FilesName.Trim().ToCharArray();
for each (char letter in letters){
sb.Append( letter == ';' ? ';' : '1' ) ;
}
if (letter != ';'){
sb.Append("1");
}
}

or something similar...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Raed Sawalha" <Ra*********@discussions.microsoft.com> wrote in message
news:98**********************************@microsof t.com...
I have the following letters;

string letters = "a;b;c....to z";

the I need to replace the incoming string which containing letters above
with integer 1 i did following

for(int u=0;u<letters.Split(';').Length;u++) {
FilesName = FilesName.Trim().Replace(letters.Split(';')[u], "1");
}

is there better way to do that


Nov 19 '05 #4

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

Similar topics

1
2173
by: Billy N. Patton | last post by:
-------- Original Message -------- Subject: <string>.replace Date: Fri, 15 Oct 2004 11:07:19 -0500 From: Billy N. Patton <b-patton@ti.com> Organization: Texas Instruments Newsgroups:...
14
3253
by: Etu | last post by:
Hi, I have a string: string c = "'abc' \"cde\", 'mno' \"xyz\","; how can I use the c.Replace(???, ???) method to have this string: "'abc' "cde", 'mno' "xyz"," that is, all the...
5
2416
by: djc | last post by:
I need to prepare a large text database field to display in an asp.net repeater control. Currently I am replacing all chr(13)'s with a "<br/>" and it works fine. However, now I also want to be able...
3
3489
by: Pascal | last post by:
bonjour hello I would like to trim a string of all its white spaces so i used myString.trim() but it doesn't work as supposed : unsecable space are remaining in the middle of my string... i...
29
2867
by: gs | last post by:
let say I have to deal with various date format and I am give format string from one of the following dd/mm/yyyy mm/dd/yyyy dd/mmm/yyyy mmm/dd/yyyy dd/mm/yy mm/dd/yy dd/mmm/yy mmm/dd/yy
8
22105
by: Warren Moxley | last post by:
Hi there, i've been searching for a C String search and replace function. I need to find all occurrences of " " in a char* array, and replace them with another char, I know how to do this in...
5
25506
by: herman | last post by:
How can I replace all occurrences of a character with another character in std string? For example, I want to replace '/' with '+' in my std::string I have looked at the replace() method in...
6
6789
by: Gabriel | last post by:
Hello, I do this : s = Environment.CurrentDirectory; s = s.Replace("\\", @"\"); Environment.CurrentDirectiry return a path like this C:\\....\\....\\..... I'd like replace the \\ by \, but...
10
18612
by: Lonifasiko | last post by:
Hi, Just want to replace character at index 1 of a string with another character. Just want to replace character at that position. I thought Replace method would be overloaded with an index...
5
2567
by: shapper | last post by:
Hello, I have a text as follows: "My email is something@something.xyz and I posted this @ 2 am" I need to replace the @ by (AT) bu only the ones that are in email addresses. All other @...
0
7091
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
7342
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...
1
6998
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
7464
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...
1
5018
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...
0
3171
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...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1516
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 ...
0
391
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.