473,569 Members | 2,772 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....t o 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.S plit(';').Lengt h;u++) {
FilesName = FilesName.Trim( ).Replace(lette rs.Split(';')[u], "1");
}

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

StringBuilder sb = new StringBuilder(l etters.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*********@di scussions.micro soft.com> wrote in message
news:98******** *************** ***********@mic rosoft.com...
I have the following letters;

string letters = "a;b;c....t o 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.S plit(';').Lengt h;u++) {
FilesName = FilesName.Trim( ).Replace(lette rs.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*********@di scussions.micro soft.com> wrote in message
news:98******** *************** ***********@mic rosoft.com...
I have the following letters;

string letters = "a;b;c....t o 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.S plit(';').Lengt h;u++) {
FilesName = FilesName.Trim( ).Replace(lette rs.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(l etters.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******** ********@TK2MSF TNGP12.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(l etters.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*********@di scussions.micro soft.com> wrote in message
news:98******** *************** ***********@mic rosoft.com...
I have the following letters;

string letters = "a;b;c....t o 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.S plit(';').Lengt h;u++) {
FilesName = FilesName.Trim( ).Replace(lette rs.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
2180
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: alt.comp.lang.learn.c-c++ I'm trying to remove the \n from a string. If I just simply locate teh char and replace it with \0 then the destructor should only...
14
3259
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 backslashes are removed.
5
2427
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 to replace TAB's with "&nbsp;"'s to preserve the user's indentation. My questions are: 1) even though I'll probably look it up before I get a...
3
3491
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 read in msdn : and notice that trim only Removes all occurrences of white space characters from the beginning and end of this instance. So what for the...
29
2881
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
22110
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 managed environments, but i'm still learning C, does anyone have a quicky function handy? or point me in the right direction on how to acccomplish this?...
5
25513
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 the string class, it does not replace all occurrences of a character with another character. http://www.cppreference.com/cppstring/index.html
6
6792
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 the code I use not work. Any idea ? Best Regards,
10
18616
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 parameter with which you can write wanted character at that position. But no, Replace method only allows replacing one known character with another. The...
5
2570
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 @ shouldn't be replaced.
0
7924
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. ...
0
8120
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
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...
0
7968
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...
1
5512
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...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
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...
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.