0

I m not able to replace the contents,help me out!!!

protected void Button1_Click(object sender, EventArgs e)
    {
        string a = "11-22-33-44-55";
        string [] Array;
        int i;
        int lnewvalue;
        lnewvalue = 2; 
        
            Array = a.Split('-');
            for (i = 0; i <= Array.Length - 1; i++)
            {
               Array[4].Replace(Array[4].ToString, lnewvalue);
           }

    }
5
Contributors
4
Replies
8
Views
7 Years
Discussion Span
Last Post by sknake
0

lnewvalue is an integer.
You must use a char or a string here.
Remark: I would not use Array as a variable name, because it is also a type in .NET. Choose a more appropriate name instead.

0
Array[4] =  Array[4].Replace(Array[4].ToString(), lnewvalue.ToString());

A few other things.
1) () invokes the method.
2) You assign the return value of .Replace() method to the string.
3) Its better not to use Array as a variable name, as there exists a class already in .NET which can cause confusion.

>>didnt see your reply while posting ddanbe:)

0

Excuse me Danny, Lighthead (Actually I don't know your real name).
Mansi please tell us your problem compilation error or fatal error in which line, etc.... it make that easier for us to help.

1

I'm with Ramy on this one ... I don't understand what your issue is exactly but here is a cleaned up version of the code that does the same thing:

private void button2_Click(object sender, EventArgs e)
    {
      string a = "11-22-33-44-55";
      string[] sArray = a.Split(new char[] { '-' });
      int lnewvalue = 2;
      //Instead if doing (i <= Array.Length-1) you can do (i < Array.Length)
      for (int i = 0; i < sArray.Length; i++)
      {
        sArray[4] = sArray[4].Replace(sArray[4].ToString(), lnewvalue.ToString());
      }
    }

And as lighthead pointed out string.Replace() does not perform an in-place replacement but rather returns the replaced string.

Votes + Comments
Exactly, a lot of people think wrong in Replace method.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.