Mothra Posted February 2, 2004 Posted February 2, 2004 Alright, I'm stuck once more. Here's the deal. I'm trying to iterate through a string and replace certain parts that are marked with a special character. The problem is that I don't want to do a general .Replace, I want to read through the string and as I come to the marked position, I want to replace the character directly following the marker with a value from an array. I guess what I really want is similar functionality to Mid() in good old VB 6 (something that will let me specify the position where the replacment should take place). Am I out of luck? Help me Obi Wan...you're my only hope... Quote Being smarter than you look is always better than looking smarter than you are.
NK2000 Posted February 2, 2004 Posted February 2, 2004 first look at the String class, it would be really helpful if you give me/us an example so we can tell you an algorythm, if you dont want to use replace then you have to work with substring i think Quote
Mothra Posted February 2, 2004 Author Posted February 2, 2004 Example... I guess that WOULD help a little.... The string I'm be using would look like this, The characters I want to replace are preceded by a '~' this wouls be taken out of a richTextBox ~A ~B ~C ~D This is some sample text ~E ~F ~G ~H This is some sample text (the real string will be formatted just like this but have many more lines...) and assigned to a string variable string myString = richTextBox1.Text; I want to replace the '~A', '~B', '~C' and '~D'(...) with values from an array, call it string[] values; The '~A' would be replaced by values[0], the '~B' would become values[1] and so on...The only trouble I'm having is getting the values into myString. Doing a blanked .Replace has the possibility of re-converting a character so I really need to run through the string and replace them as I come to them. Quote Being smarter than you look is always better than looking smarter than you are.
NK2000 Posted February 2, 2004 Posted February 2, 2004 string text = " .... "; for (int i=0; i < text.Length; i++) { if (text.Chars[i] == '~') text = text.Substring(0, i) + values[i] + text.Substring(i+1); } i didnt test it, but tell me if any error occurs Quote
Mothra Posted February 2, 2004 Author Posted February 2, 2004 String does not have a .Chars[]...I think, though, if I convert the string to a char[] and then iterate through the char[] and replace the individual characters as I come to them. I'm trying to make that work right now.... Quote Being smarter than you look is always better than looking smarter than you are.
akiaz Posted February 2, 2004 Posted February 2, 2004 Use the Regex.Replace method to find and exchange your ~X code. You'll need one for each code you want to swap. I don't see a reason to iterate through the string letter by letter if you use this route. Quote
Mothra Posted February 3, 2004 Author Posted February 3, 2004 I'll check that out but, I did find a solution. I converted the string into a character array and that seemed to do the trick. I might play with the Regex.Replace but, since I got the logic to work with the char[] I'm not sure I'll change it now... Thank you both for all your help! Quote Being smarter than you look is always better than looking smarter than you are.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.