what takes more system resources?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to convert text. lots of it.. like over 100 different characters...

TextToConvert = TextToConvert.Replace("2", "ï¼’");
TextToConvert = TextToConvert.Replace("3", "3");
TextToConvert = TextToConvert.Replace("4", "ï¼”");
TextToConvert = TextToConvert.Replace("5", "5");
TextToConvert = TextToConvert.Replace("6", "ï¼–");
TextToConvert = TextToConvert.Replace("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", "ï¼’");
}
if(TextToConvert.Contains("2")
{
TextToConvert = TextToConvert.Replace("3", "3");
}
if(TextToConvert.Contains("2")
{
TextToConvert = TextToConvert.Replace("4", "ï¼”");
}
if(TextToConvert.Contains("2")
{
TextToConvert = TextToConvert.Replace("5", "5");
}
if(TextToConvert.Contains("2")
{
TextToConvert = TextToConvert.Replace("6", "ï¼–");
if(TextToConvert.Contains("2")
{
TextToConvert = TextToConvert.Replace("7", "ï¼—");
}
if(TextToConvert.Contains("2")
{
TextToConvert = TextToConvert.Replace("8", "8");
}
etc...
 
Ouch! How about:

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

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

Marc
 
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) {
case '0': sb = '?'; break;
case '1': sb = '?'; break;
case '2': sb = '?'; break;
case '3': sb = '?'; break;
case '4': sb = '?'; break;
// etc...
}
}
return sb.ToString();
}

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

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

That could possibly reduce the size of your switch statement.

rossum
 
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.
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top