473,471 Members | 1,684 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

String - Optimization questions..

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[i] == 'c')
{
chars[i] = '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[i] == 'c')
{
s[i] = 'x';
}
}
//***

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

thanks for the help..

--
Best Regards
Yanick Lefebvre

Please posts answers to the group so all can benefit
Nov 15 '05 #1
12 1801
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.
"Zoury" <yanick_lefebvre at hotmail dot com> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
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[i] == 'c')
{
chars[i] = '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[i] == 'c')
{
s[i] = 'x';
}
}
//***

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

thanks for the help..

--
Best Regards
Yanick Lefebvre

Please posts answers to the group so all can benefit

Nov 15 '05 #2
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[i] == 'c'

chars[i] = '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[i] == 'c'

s[i] = 'x'
//**

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

thanks for the help.

--
Best Regard
Yanick Lefebvr

Please posts answers to the group so all can benefi

Nov 15 '05 #3
> 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)

--
Best Regards
Yanick Lefebvre

Please posts answers to the group so all can benefit
Nov 15 '05 #4
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[i] == 'c')
{
chars[i] = 'x';
}
}
s = new string(chars);
//***


there.. ;O)

--
Best Regards
Yanick Lefebvre

Please posts answers to the group so all can benefit
Nov 15 '05 #5
"Zoury" <yanick_lefebvre at hotmail dot com> wrote:

<snip>
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[i] == 'c')
{
s[i] = '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[i] == 'c') {
sb[i] = '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>
Nov 15 '05 #6
<"Zoury" <yanick_lefebvre at hotmail dot com>> wrote:
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[i] == 'c')
{
chars[i] = '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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
> > 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)


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.
Nov 15 '05 #8
Martin Maat [EBL] <du***@somewhere.nl> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9
> 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?

--
Best Regards
Yanick Lefebvre

Please posts answers to the group so all can benefit
Nov 15 '05 #10
Thanks to all of you!

--
Best Regards
Yanick Lefebvre

Please posts answers to the group so all can benefit
Nov 15 '05 #11
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)

--
Best Regards
Yanick Lefebvre

Please posts answers to the group so all can benefit
Nov 15 '05 #12
ok thanks!

--
Best Regards
Yanick Lefebvre

Please posts answers to the group so all can benefit
Nov 15 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Gaurav | last post by:
Hello I have a program that basically inverts the contents of files except first line. It compiles fine but gives me core dump on running. If i comment temp.clear() it runs fine, but i need...
14
by: brad | last post by:
I've got a multithreaded application using std::string in Linux. Performance is not very good so I ran Quantify(tm) to look at what is happening. Most of the time my app was calling...
9
by: Rune | last post by:
Is it best to use double quotes and let PHP expand variables inside strings, or is it faster to do the string manipulation yourself manually? Which is quicker? 1) $insert = 'To Be';...
5
by: Praveen_db2 | last post by:
Dear All Db2 version: 8.1 OS: Windows I have 2 questions: 1) What is the optimizer which db2 uses, rule based or cost based? If any one can clear out the difference between the two it will be...
22
by: spike | last post by:
How do i reset a string? I just want to empty it som that it does not contain any characters Say it contains "hello world" at the time... I want it to contain "". Nothing that is.. Thanx
9
by: Michael D. Ober | last post by:
OK, I can't figure out a way to optimize the following VB 2005 code using StringBuilders: Public Const RecSize as Integer = 105 Private buffer As String Public Sub New() init End Sub...
42
by: =?Utf-8?B?UGxheWE=?= | last post by:
I have an if statement that isn't working correctly and I was wondering how I check for a blank string. My Code Example if me.fieldname(arrayIndex) = "" then ----- end if When I do this and...
42
by: Armin | last post by:
Hi, just a dumb question. Let a = Why is the value of a.append(7) equal None and not ?? --Armin
20
by: Ravikiran | last post by:
Hi Friends, I wanted know about whatt is ment by zero optimization and sign optimization and its differences.... Thank you...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.