|
how fast or slow is it? how did you establish that? is that for the first insert only, or have all your insert commands such speed? and how fast do you want it to be?
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
|
|
|
|
|
If you execute this code many times you can probably hasten it by using parameters (and eliminating the ToString s).
|
|
|
|
|
Hi,
There is a string such as:
string s = "12445-65432 abc, 98767-45364 th, 56749-23198"
How can I remove the letters so that I end up with:
s = "12445-65432, 98767-45364, 56749-23198"
Thanks
|
|
|
|
|
|
Use
Regex.Replace("your String", "[A-Za-z]", "");
himanshu
|
|
|
|
|
forget regex, simple function...
public string RemoveLetters(string original)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for(int i = 0; i < original.Length; i++)
{
if(!char.IsLetter(original, i))
sb.Append(original[i]);
}
return sb.ToString();
}
EDIT:
my answer does exactly what is required, how it get 2 'bad answer' hits?
3.0 - getting better...
4.0 - much more respectable, thou it can never be a 5...
Life goes very fast. Tomorrow, today is already yesterday.
modified on Thursday, June 25, 2009 10:59 AM
|
|
|
|
|
At the risk of starting a flamewar how is
public string RemoveLetters(string original)<br/>{<br/> System.Text.StringBuilder sb = new System.Text.StringBuilder();<br/> for(int i = 0; i < original.Length; i++)<br/> {<br/> if(!char.IsLetter(original, i))<br/> sb.Append(original[i]);<br/> }<br/> return sb.ToString();<br/>}
Simpler than:
Regex.Replace("your String", "[A-Za-z]", "");
Despite the common belief that string builders are better, they are inefficient for most applications, it'd be much quicker cycling through an array of chars (a-z) (or their equivalient integer values & converting) and removing them in a loop.
The regex will allow other caracters he'll probably need to remove to be added with ease.
|
|
|
|
|
I didn't say it was simpler, just there is a simple way of doing it yourself.
If you loop the chars you will have to create a new string with each remove anyway. I don't know the exact performance difference so I'm not trying to argue its better in that respect. I just basically like to give answers that show the logic involved (when its a simple task like this), that way people can learn the logic and apply that when 'out the box' functions either are not available or simple don't do exactly what you want.
For example, lets say the OP really wants to do this task over a 1GB file with a background worker. Now obviously you don'd load 1GB into a string, you would do a ReadByte loop or something similar, but my method shows how easy it is to detect if a character is a letter and then progress can be reported after each loop (or 1000 loops etc.)
Don't get me wrong, was not trying to start an argument, both Regex suggestions are perfectly valid - just wanted to give an alternative
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Probably because this is a homework / study question and regex is out of the scope of the educational material for an intro / basic course. The teacher will be looking for a students understanding of the logic of loops and methods within the char class over regex at this point.
|
|
|
|
|
So actually giving them the Regex answer is a Good Thing, as the teacher will instantly know that they have shown no understanding of the problem and have simply asked an online forum for the answer.
Answering this questions correctly is getting more and more difficult.
|
|
|
|
|
J4amieC wrote: So actually giving them the Regex answer is a Good Thing, as the teacher will instantly know that they have shown no understanding of the problem and have simply asked an online forum for the answer.
No, the exam will prove that by itself.
|
|
|
|
|
Hi,
the regex approach is easy to write, however it is not very CPU-efficient; in my test it was between 8 and 9 times slower than the StringBuilder plus for-loop approach. And StringBuilder *is* the right approach when one has to perform a reasonable number of concatenations, since, without them, each operation would have to create a new string and copy its content (a quadratic cost).
When more complex character collections have to be removed, isAlpha is not useful anymore, IndexOf could be the right approach; it would slow down the for-loop by a factor of 2, however the regex also slows down when adding to the search set.
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
|
|
|
|
|
I din't vote you down, but the first line of your answer I didn't like.
"Forget regex" sounds like you were saying regex was not the right choice - in actual fact it is certainly something you should consider for this sort of task. But I think you know that and the "forget regex" was a flippant remark.
Anyway, it was a good enough answer.
|
|
|
|
|
your right... I meant no offence to Regex or it's users
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Hi,
Any idea how to capture a router's amount of data transferred (Sent/Received)?? You know, you're browsing the internet and want to know how many bytes you've used so far since morning for example..
Thank you.
All generalizations are wrong, including this one!
(\ /)
(O.o)
(><)
|
|
|
|
|
You could try UPnP..
Or just use Networx[^] if you just want to know the number and don't mind not having programmed it yourself
|
|
|
|
|
|
I use NetMeter[^] which gives daily, weekly, monthly and complete totals (for your PC, not the router which may be shared). Freeware, works fine.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
Hi.
I have created a file with ".abc" extension..
double clicking on that file im opening mdi application and displaying the data in child window.
if i double click on another file it is not opening in new mdi child.
please help me how to proceed to open new child window in an mdi app on double clicking the file..
thanks
|
|
|
|
|
how to make application forms lookable? pls help
|
|
|
|
|
|
harold aptroot wrote: What does that mean?
The correct answer: LIQUID NITROGEN[^]
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
one way is to use AjaxControlToolkit (in web application)
modified on Thursday, June 25, 2009 8:20 AM
|
|
|
|
|
How do you get this answer from the question posted? Do you have some form of mind-reading skills?
|
|
|
|
|
Serv-dee wrote: how to make application forms lookable?
Switch the monitor on. If it's off, it's not lookable.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|