This is an archived, read-only copy of the United-TI subforum , including posts and topic from May 2003 to April 2012. If you would like to discuss any of the topics in this forum, you can visit Cemetech's
Calculator Programming subforum. Some of these topics may also be directly-linked to active Cemetech topics. If you are a Cemetech member with a linked United-TI account, you can link United-TI topics here with your current Cemetech topics.
General Coding and Design =>
Calculator Programming
Author |
Message |
|
Newbie
Bandwidth Hog
Joined: 23 Jan 2004 Posts: 2247
|
Posted: 13 Nov 2009 12:51:35 pm Post subject: |
|
|
This code is weird. I want it to remove any characters from a string that is not A-Z or 0-9
It does it's job unless you add a bunch of commas to at the end of a string and then it won't delete all of them.
Add a string like: Th,is,,is, a ,string.,,,,,,,,
It will delete the commas in the sentence but not at the end. Why?
Code: for (int position = 0; position <= strEditedPlot.Length - 1; position++)
{
// Convert to ascii character to compare value
int ascii = Convert.ToInt16(char.Parse(strEditedPlot.Substring(position, 1)));
if (ascii <= 58 && ascii >= 33 || ascii <= 25 && ascii >= 16)
{
strEditedPlot = strEditedPlot.Remove(position, 1);
}
}
|
|
Back to top |
|
|
benryves
Active Member
Joined: 23 Feb 2006 Posts: 564
|
Posted: 13 Nov 2009 02:20:23 pm Post subject: |
|
|
That code is rather oddly written, to put it politely. To retrieve a character at a particular position in a string, use the indexer (strEditedPlot[position]). Don't convert characters to ASCII then compare against magic numbers - someChar >= 'A' && someChar <= 'Z' would work instead, for example. It would also be more efficient (not to mention easier to read) to check characters one by one and, if they pass, append them to new string in a StringBuilder rather than removing them from the old string. Strings in .NET are immutable (="cannot be changed") so whenever you modify one it creates a new copy of it. StringBuilders are mutable, however.
In any case, using a regular expression would make this all a lot simpler!
Code: strEditedPlot = new Regex("[^A-Z0-9]").Replace(strEditedPlot, "");
Character groups are put in square brackets, and a ^ at the start of a group inverts it, so that code reads as "replace any character that is not in the range A-Z or 0-9 with the empty string".
Last edited by Guest on 13 Nov 2009 02:21:22 pm; edited 1 time in total |
|
Back to top |
|
|
Newbie
Bandwidth Hog
Joined: 23 Jan 2004 Posts: 2247
|
Posted: 13 Nov 2009 02:27:57 pm Post subject: |
|
|
Thanks.
Didn't know that Regex existed and thus why I didn't use it. I guess if your not familiar with every namespace it can be easy to overlook something.
Last edited by Guest on 13 Nov 2009 02:30:42 pm; edited 1 time in total |
|
Back to top |
|
|
|
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.
»
Go to Registration page