By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,158 Members | 1,325 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,158 IT Pros & Developers. It's quick & easy.

what takes more system resources?

P: n/a
I need to convert text. lots of it.. like over 100 different characters...

TextToConvert = TextToConvert.Replace("2", "2");
TextToConvert = TextToConvert.Replace("3", "3");
TextToConvert = TextToConvert.Replace("4", "4");
TextToConvert = TextToConvert.Replace("5", "5");
TextToConvert = TextToConvert.Replace("6", "6");
TextToConvert = TextToConvert.Replace("7", "7");
TextToConvert = TextToConvert.Replace("8", "8");
etc...

is there a faster way to do this?

if this is the only way to do this, then what takes more resources? straight
conversion no matter what, or comparing then converting like below....

if(TextToConvert.Contains("2")
{
TextToConvert = TextToConvert.Replace("2", "2");
}
if(TextToConvert.Contains("2")
{
TextToConvert = TextToConvert.Replace("3", "3");
}
if(TextToConvert.Contains("2")
{
TextToConvert = TextToConvert.Replace("4", "4");
}
if(TextToConvert.Contains("2")
{
TextToConvert = TextToConvert.Replace("5", "5");
}
if(TextToConvert.Contains("2")
{
TextToConvert = TextToConvert.Replace("6", "6");
if(TextToConvert.Contains("2")
{
TextToConvert = TextToConvert.Replace("7", "7");
}
if(TextToConvert.Contains("2")
{
TextToConvert = TextToConvert.Replace("8", "8");
}
etc...
Jan 25 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a

Ouch! How about:

string output = Regex.Replace(TextToConvert, @"(\d)", @" $1 ");

If this doesn't do what you need, then provide more info...

Marc

Jan 25 '07 #2

P: n/a
On Thu, 25 Jan 2007 13:00:02 -0800, roger_27
<ro*****@discussions.microsoft.comwrote:
>I need to convert text. lots of it.. like over 100 different characters...

TextToConvert = TextToConvert.Replace("2", "?");
TextToConvert = TextToConvert.Replace("3", "?");
TextToConvert = TextToConvert.Replace("4", "?");
TextToConvert = TextToConvert.Replace("5", "?");
TextToConvert = TextToConvert.Replace("6", "?");
TextToConvert = TextToConvert.Replace("7", "?");
TextToConvert = TextToConvert.Replace("8", "?");
etc...

is there a faster way to do this?
Yes. You are scanning the entire text string once for each character,
so you will scan it over 100 times.
>
if this is the only way to do this, then what takes more resources? straight
conversion no matter what, or comparing then converting like below....

if(TextToConvert.Contains("2")
{
TextToConvert = TextToConvert.Replace("2", "?");
}
if(TextToConvert.Contains("3")
{
TextToConvert = TextToConvert.Replace("3", "?");
}
No, this is slower. If the character is present then you are doing a
partial scan for Contains() and a full scan for Replace(). If the
character is not present then you are doing a full scan for
Contains(). This will be slower than your first method, unless none
of the characters are present.

A better way is to convert the string into a StringBuilder so you can
work on it more easily; it allows you to change characters in place in
the middle of the string. You only have to scan the string once.
This will also use a lot less memory as StringBuilders are not
immutable like strings.

string Convert(string textToConvert) {
StringBuilder sb = new StringBuilder(textToConvert);
for (int i = 0; i < sb.Length; ++i) {
switch (sb[i]) {
case '0': sb[i] = '?'; break;
case '1': sb[i] = '?'; break;
case '2': sb[i] = '?'; break;
case '3': sb[i] = '?'; break;
case '4': sb[i] = '?'; break;
// etc...
}
}
return sb.ToString();
}

Depending on exactly what you want you might find IsDigit() and
similar functions useful:

if (char.IsDigit(sb[i])) { sb[i] = '?'; }

That could possibly reduce the size of your switch statement.

rossum

Jan 25 '07 #3

P: n/a
Okay... chinese characters are twice as wide as english letters. but what if
you are typing english ??

it needs to make them into their chinese character counterparts of roman
letters. because they are double width also.

two english letters, makes 1 chinese character (we're speaking in fixed
width font form). so I need to replace any instances of a single width
character like "A" with "A" (you need the chinese language pack installed to
view this correctly).

I need to do it for all 26 letters lowercase, then all 26 letters uppercase,
then symbols like @#$%^ etc.. they all have a double width counterpart.

any help is appreciated.
thanks.

"Marc Gravell" wrote:
>
Ouch! How about:

string output = Regex.Replace(TextToConvert, @"(\d)", @" $1 ");

If this doesn't do what you need, then provide more info...

Marc

Jan 25 '07 #4

P: n/a
>From the other replies, there may have been some encoding issues
throwing me off the scent, in which case I apologies... I received "1"
-" 1 ", "2" -" 2 ", etc... in which case Regex is the way to go.
For the more involved example - have you googled? Is there a specific
name for this conversion?

Marc

Jan 26 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.