Jump to content
  • Advertisement
Sign in to follow this  
Stompy9999

[.net] Find each char in a string

This topic is 4803 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm writing a C# program that requires each char in a string to be read and translated into a symbol. Would this be a valid method for accomplishing this:
public string Translate(string some_string)
{
     
     string new_string;     

     foreach(char c in some_string)
     {
             char translated_char;
             // Translate char
             new_string += translated_char;
     }

     return new_string;

}

Sorry if this isn't valid C# code, I origionally wrote this in C++, and am trying to rewrite it in C#. Would this work?

Share this post


Link to post
Share on other sites
Advertisement
That looks like it compiles and runs just fine with C# and should do what you need it to. You will have to have new_string = ""; though. C# requires all objects to be initialized.

Another way of doing that would be kinda like C++ syntax [wink]


public string Translate( string some_string )
{
string new_string = "";
for (int x = 0; x < some_string.Length; x++ )
{
char translated_char = some_string[x];
...
new_string += translated_char;
}
return new_string;
}






Of course you don't need to index access since the variable 'c' in your example represents the current character in the string as it iteratres though, but it is another way.

Share this post


Link to post
Share on other sites
do not use a String object to build your string, it'll totally destroy your memory. Specifically, for a size n string, you'll end up using O(n^2) more bytes of memory building it one character at a time. Remember, Strings are immutable, a new string is created any time you "edit" a string.

Use a StringBuilder.

Share this post


Link to post
Share on other sites
Taking into what capn_midnight said, this would probably be a better solution:


public StringBuilder Translate(string some_string)
{

StringBuilder new_string = new StringBuilder();

for(int i = 0; i < some_string.Length; i++)
{
char translated_char = some_string;
// Translate char
new_string.Append(translated_char);
}

return new_string;

}




Once again, please correct me if I'm wrong.

Share this post


Link to post
Share on other sites
should be good, stompy

if you do need a String object out of it, it's pretty simple, just call the ToString() method, it's overridden to return a proper string instead of a string representation of the object reference.


public string Translate(string some_string)
{

StringBuilder new_string = new StringBuilder();
foreach(char c in some_string) new_string.Append(doTranslation(c));
return new_string.ToString();
}

You should check, but it might even do an implicit cast, so you wouldn't even have to call the ToString() method when returning.

Share this post


Link to post
Share on other sites
It might also be a good idea to initialize the StringBuilder with the size of your string so it only does allocation once.

Share this post


Link to post
Share on other sites
Quote:
Original post by capn_midnight
do not use a String object to build your string, it'll totally destroy your memory. Specifically, for a size n string, you'll end up using O(n^2) more bytes of memory building it one character at a time. Remember, Strings are immutable, a new string is created any time you "edit" a string.

Use a StringBuilder.


Thanks for that tip Capn [wink]. I see what is meant now with how the string class works. I think I need to go back and start updating a lot of C# code that I wrote that uses the C++ std:;string methodology now!

Share this post


Link to post
Share on other sites
If you are going to be doing much string manipulation you may want to take the time to learn the basics of regular expressions -- they are very good for doing just this sort of thing. For Example:

string new_string = Regex.Replace(some_string,".",new MatchEvaluator(Translate));
string Translate(string){ /*your translation here*/ }

Will call translate on every character in some_string and return a translated result string. The thing about regular expressions is that they are very flexable and you can do much much more with them. If you wanted to allow for an escaping scheme in your translator you could do replace the "." with "(#.|.)" so that characters preceeded with a pound sign are interpreted as single unit when passed to translate.

Expressions can also be compiled and, if you use them correctly, very fast.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!