[.net] string.replace(...) help

Started by
6 comments, last by BradSnobar 18 years, 1 month ago
Hi guys, i am seeking one problem right now. I want to somehow "decode" the text what user entered, with replace method. Here is my source: =============================================================== static void Main(string[] args) { string text; Console.Write("Please say something: "); text = Console.ReadLine(); Console.WriteLine("{0}", text.Replace("a", "1")); Console.ReadLine(); } =============================================================== BUT the problem is that i dont know how can i make more text been replaced by more numbers. Now the every "A" in the text, will be replace by "1", but how to make this: A=1 B=2 C=3 ... NOT WORKING ->Console.WriteLine("{0}", text.Replace("a" "b", "1" "2")); Thanks in futher! Sorry for my bad english!
Advertisement
Here is one way.. it may not be elegant but works..

string text;            Console.Write("Please say something: ");            text = Console.ReadLine();            String decoded = String.Empty;            foreach (Char letter in text.ToCharArray())            {                switch (letter)                {                    case 'a':                        decoded += "1";                        break;                    case 'b':                        decoded += "2";                        break;                    case 'c':                        decoded += "3";                        break;                        // etc.. etc..                    default:                        decoded += letter.ToString();                        break;                }                            }            Console.WriteLine("{0}", decoded);                        Console.ReadLine();
Quote:Original post by XeeRox
Hi guys, i am seeking one problem right now.
I want to somehow "decode" the text what user entered, with replace method.

Here is my source:
===============================================================
static void Main(string[] args)
{
string text;

Console.Write("Please say something: ");
text = Console.ReadLine();

Console.WriteLine("{0}", text.Replace("a", "1"));
Console.ReadLine();
}
===============================================================
BUT the problem is that i dont know how can i make more text been replaced by more numbers.

Now the every "A" in the text, will be replace by "1", but how to make this:

A=1
B=2
C=3
...

NOT WORKING ->Console.WriteLine("{0}", text.Replace("a" "b", "1" "2"));

Thanks in futher!
Sorry for my bad english!


JustAddWater's solution is viable, and probably the way you want to go if you want to keep it simple.

The other way to go is regular expressions. 4GuysFromRolla has an article that explains them. It's not the best article in the world but to be honest, regular expressions can be a real pain to understand fully.

You're looking for Regex.Replace. I can post a sample when I get to work if you'd like.
..what we do will echo throughout eternity..
Or more simply (if more ugly and hackish):
    public static string Encode(string text) {        string rtn = "";        foreach (Char c in text) {            rtn += ((int)((int)c - (int)'a' + 1)).ToString();        }        return rtn;    }
Why not just have a simple for loop?

for(int i = 0, k = 64; k < 96; k++, i++){     text = text.Replace((char)k, i.ToString());}


That is pseudo code... the idea is use ascii codes to convert letters to numbers from 1..*
Quote:Original post by Krisc
Why not just have a simple for loop?

for(int i = 0, k = 64; k < 96; k++, i++){     text = text.Replace((char)k, i.ToString());}


That is pseudo code... the idea is use ascii codes to convert letters to numbers from 1..*


That works, but bear in mind that strings are immutable in C# so you're creating a massive number of temporary strings with that loop.

..what we do will echo throughout eternity..
Quote:Original post by Telastyn
Or more simply (if more ugly and hackish):
*** Source Snippet Removed ***

One change, per the above note.
public static string Encode(string text) {        System.Text.StringBuilder rtn = new System.Text.StringBuilder();        foreach (Char c in text) {            rtn.Append(((int)((int)c - (int)'a' + 1)).ToString());        }        return rtn.ToString();    }
..what we do will echo throughout eternity..
The faster solution here is to use a hashtable (or a dictionary<> if .net 2.0)
This can also be used for other types of translation, and it doesn't depend on any tricks so it is very maintable.
  class Program  {    static void Main(string[] args)    {      System.Collections.Hashtable hash = new System.Collections.Hashtable();      hash['a'] = 1;      hash['b'] = 2;      hash['c'] = 3;            string source = "This is a test string";      System.Text.StringBuilder target = new System.Text.StringBuilder();      char[] ca = source.ToCharArray();      foreach (char c in ca)      {        if (hash[c] != null)          target.Append(hash[c]);        else          target.Append(c);      }      System.Console.WriteLine(target);      System.Console.ReadLine();    }  }

This topic is closed to new replies.

Advertisement