-1

Basically our Problem is: We can't replace a string like this: 10003* But we can replace a string like this: 10003

we want to replace a part of a string that looks like this: 10003* This is our Code:

string text = sr2.ReadToEnd();
sr2.Close();
while (loop != lstTxt.Items.Count)
{
    string SelectedItem = lstTxt.SelectedItem.ToString() + "*";

    text = text.Replace(SelectedItem, "tietze111");


    if (lstTxt.SelectedIndex < lstTxt.Items.Count - 1)
        lstTxt.SelectedIndex++;
    loop++;

}

sw2.Write(text);

But it doesn't work. When we leave out the * in the part to replace, it works. But we have to replace the * too. Do you know what we have to change?

It works when we use this:

string text = sr2.ReadToEnd();
sr2.Close();
while (loop != lstTxt.Items.Count)
{
    string SelectedItem = lstTxt.SelectedItem.ToString(); // changed

    text = text.Replace(SelectedItem, "tietze111");


    if (lstTxt.SelectedIndex < lstTxt.Items.Count - 1)
        lstTxt.SelectedIndex++;
    loop++;

}

sw2.Write(text);

-- using (var sr2 = new StreamReader(Application.StartupPath + @"\website\Dehler 22 ET.htm", Encoding.Default)) {

                using (var sw2 = new StreamWriter(tempFile, true, Encoding.Default))

We are using this because the file is still in ASCII. Maybe that is the problem. How do we solve this?

2
  • A System.String is a sequence of unicode characters, so when you read the file it is converted to unicode. Debug and set a breakpoint, check that the value is what it should be. Perhaps decode it to byte array, and see if data or the substitutionkeys are not what you think... ('*' is a character that is defined in ASCII encoding.) Commented May 1, 2012 at 6:38
  • Did you ever get this working? Commented May 4, 2012 at 13:27

4 Answers 4

0

Fix the following line,

string SelectedItem = lstTxt.SelectedItem.Value;

You are taking the item and not the value.

Sign up to request clarification or add additional context in comments.

3 Comments

Basically our Problem is: - We can't replace a string like this: 10003* - But we can replace a string like this: 10003
Could it be a problem of encoding ? You might be mixing UTF-8 and ASCII characters.
Did you copy the valur from something like a Word document, in that case it's most definitely a problem of mixing the encodings. IN which case you simply need to remove and write the asterik again. In other case : From where are you getting this value that's 10003* ?
0

Have you tried?

"[\*]"

Or

@"[*]" 

1 Comment

not sure if this'll work or not.. just something I found online
0

The String.Replace(String,String) method doesn't do anything special with any characters. There is a character in your id that you are trying to replace is not the same as the one you are trying to match on. I would try copying the astrisk from the data source into your code and see if the problem still exists.

1 Comment

have you set a breakpoint in your code to see what is actually stored in text before you try the replacement?
0

Your problem * is encoded in some other type. The Unicode value for asterisk U+002A

You could try this. Note Char.MinValue is technically a null value since you cannot have a blank Char.

In your case: lstTxt.SelectedItem.ToString() + '\u002A'.ToString();

If that doesn't work try removing (using different encoded values for *) to make sure you can actually find it in the string.

SomeString.Replace('\u002A', Char.MinValue);

OR

SomeString.Replace('\u002A'.ToString(), String.Empty);

I've ran into issues like this before and it ends up being a trial and error kind of thing until you get it right. Similar problem I had last summer C# String.Replace not finding / replacing Symbol (™, ®)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.