Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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...

Being smarter than you look is always better than looking smarter than you are.
Posted
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
Posted

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.

Being smarter than you look is always better than looking smarter than you are.
Posted

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

Posted
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....
Being smarter than you look is always better than looking smarter than you are.
Posted
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.
Posted
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!
Being smarter than you look is always better than looking smarter than you are.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...