String - Optimization questions..


Z

Zoury

Hi there! :O)

I need to replace all the accentued character of a string by it's
non-accentued-character equivalent.

1. is there a way to do so without iterating trought the entire string and
replacing character, with the Convert or Encoding class for example?
2. if not, how can you actually replace a specific character in a string
without recreating a new string?

for now I do something like..
//***
string s = "aaaa ccaaa c aaa ca aaa";
char[] chars = s.ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
if (chars == 'c')
{
chars = 'x';
}
s = new string(chars);
}
//***

this is seems to be the lighter way we've found to do it... :O/

I would done something like this but it's not possible instead but the
compiler is complaining about the indexer being read-only..... (wich is
poor..)
//***
string s = "aaaa ccaaa c aaa ca aaa";

for (int i = 0; i < s.Lenght; i++)
{
if (s == 'c')
{
s = 'x';
}
}
//***

and I am **not** looking for the Replace() function...

thanks for the help..
 
Ad

Advertisements

M

Marina

Why don't you want the Replace function? It was created so that you wouldn't
have to write loops like you have there.

And if you must use the loop, why are you recreated the string at the end of
every loop? What is the point? You are always using just the last one, so
recreate the string from the char array once the loop is finished, no reason
to do it 20 times for 20 characters.
 
G

Guest

Zoury
Strings are immutable. If you want to replace characters in a string, then a new string must be created

Tu-Thac

----- Zoury wrote: ----

Hi there! :O

I need to replace all the accentued character of a string by it'
non-accentued-character equivalent

1. is there a way to do so without iterating trought the entire string an
replacing character, with the Convert or Encoding class for example
2. if not, how can you actually replace a specific character in a strin
without recreating a new string

for now I do something like.
//**
string s = "aaaa ccaaa c aaa ca aaa"
char[] chars = s.ToCharArray()
for (int i = 0; i < chars.Length; i++

if (chars == 'c'

chars = 'x'

s = new string(chars)

//**

this is seems to be the lighter way we've found to do it... :O

I would done something like this but it's not possible instead but th
compiler is complaining about the indexer being read-only..... (wich i
poor..
//**
string s = "aaaa ccaaa c aaa ca aaa"

for (int i = 0; i < s.Lenght; i++

if (s == 'c'

s = 'x'


//**

and I am **not** looking for the Replace() function..

thanks for the help.
 
Z

Zoury

Strings are immutable. If you want to replace characters in a string,
then a new string must be created.

hhmmm... :O/
... thanks for the info.. :O)
 
Z

Zoury

Hi Marina! :O)
Why don't you want the Replace function? It was created so that you wouldn't
have to write loops like you have there.

Because the sample shown wasn't really what is was trying to do... i don't
konw yet how many characters i'll have to replace, so calling the replace
function in a loop let say 50 times to change fifty differents accents
sounded a little bit overhead to me..
And if you must use the loop, why are you recreated the string at the end of
every loop? What is the point? You are always using just the last one, so
recreate the string from the char array once the loop is finished, no reason
to do it 20 times for 20 characters.

that's my mistakes.. sorry
the actual code doesn't recreate it *in* the loop but really outside it :
for now I do something like..
//***
string s = "aaaa ccaaa c aaa ca aaa";
char[] chars = s.ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
if (chars == 'c')
{
chars = 'x';
}
}
s = new string(chars);
//***


there.. ;O)
 
C

C# Learner

"Zoury" <yanick_lefebvre at hotmail dot com> wrote:

I would done something like this but it's not possible instead but the
compiler is complaining about the indexer being read-only..... (wich is
poor..)
//***
string s = "aaaa ccaaa c aaa ca aaa";

for (int i = 0; i < s.Lenght; i++)
{
if (s == 'c')
{
s = 'x';
}
}
//***


This is because a string variable holds a reference, not a value.

If the above was possible, this could happen:

<code>

1 string a, b;
2
3 a = "hello world";
4 b = a;
5
6 a[0] = 'j';
7
8 Console.WriteLine(b); // this would output "jello world";

</code>

If this was allowed, the output would be "jello world" because in line
4, b is assigned a's *reference*, not a's *value*.

Sure, you can say a = "new string" and it won't affect what b
references, because this will create a whole new string in a different
memory location, and _a_ will now reference this new string, and _b_
stays untouched.

e.g.:

<code>

1 string a, b;
2
3 a = "hello world";
4 b = a;
5
6 a = "new string";
7
8 Console.WriteLine(b); // this would output "hello world";

</code>

If you're going for optimization, I'd expect (though I'm not sure) a
StringBuilder instance to be more efficient in this case.

e.g.:

<code>

StringBuilder sb = new StringBuilder(s);

for (int i = 0; i < sb.Length; ++i) {
if (sb == 'c') {
sb = 'x';
}
}

s = StringBuilder.ToString();

</code>

Now, the above works, since a StringBuilder instance isn't immutable,
whereas a String instance is.

e.g.:

<code>

StringBuilder a, b;

a = new StringBuilder("hello world");
b = a;

a[0] = 'j';

Console.WriteLine(b); // outputs "jello world"

</code>

This is perfectly valid.

StringBuilder can be thought of as a replacement for String (string)
for when you need mutability (the ability to change the value the
string is referencing).

<snip>
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

I need to replace all the accentued character of a string by it's
non-accentued-character equivalent.

1. is there a way to do so without iterating trought the entire string and
replacing character, with the Convert or Encoding class for example?

No. Strings are immutable.
2. if not, how can you actually replace a specific character in a string
without recreating a new string?

No - see the above.
for now I do something like..
//***
string s = "aaaa ccaaa c aaa ca aaa";
char[] chars = s.ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
if (chars == 'c')
{
chars = 'x';
}
s = new string(chars);
}


And that's the best you can do - although you might want to check
whether or not you've got anything to replace before you bother
creating a copy.
this is seems to be the lighter way we've found to do it... :O/

I would done something like this but it's not possible instead but the
compiler is complaining about the indexer being read-only..... (wich is
poor..)

It's not poor at all - the alternative is for strings to be mutable,
which would be *much* worse, IMO. You'd need to make a copy every time
you wanted to pass a string into a method without risking have it
changed, etc.
 
M

Martin Maat [EBL]

Strings are immutable. If you want to replace characters in a string,
hhmmm... :O/
.. thanks for the info.. :O)

Strings are immutable indeed, but that is what StringBuilder is for. It
allows the internal tinkering you want through a Replace method and a Chars
collection.

I don't see the point in sinking that low though, the risks and the trouble
do not seem to be worth the supposed performance gain. I would try
System.Text.Encoding, it seems perfect for the job and I seriously doubt you
can do better than the writers of that class.

Martin.
 
J

Jon Skeet [C# MVP]

Martin Maat said:
I don't see the point in sinking that low though, the risks and the trouble
do not seem to be worth the supposed performance gain. I would try
System.Text.Encoding, it seems perfect for the job and I seriously doubt you
can do better than the writers of that class.

No, System.Text.Encoding is for something entirely different -
conversions between characters and bytes. The OP never mentioned
anything to do with a byte-encoded version of the text.
 
Z

Zoury

Strings are immutable indeed, but that is what StringBuilder is for. It
allows the internal tinkering you want through a Replace method and a Chars
collection.

Naaaa... thanks for the info! ;O)
I would try
System.Text.Encoding, it seems perfect for the job and I seriously doubt you
can do better than the writers of that class.

*no* doubt about it.. <g>
but do you have a clue on how to do it using this class?
 
Ad

Advertisements

Z

Zoury

Hi Jon! :O)
It's not poor at all - the alternative is for strings to be mutable,
which would be *much* worse, IMO. You'd need to make a copy every time
you wanted to pass a string into a method without risking have it
changed, etc.

Now that I know about the StringBuilder class, I have to agree with you..
they did provide both ways to work with strings, wich is just plain
perfect... ;O)
 
Ad

Advertisements


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

Top