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);
           }

    }

Recommended Answers

All 4 Replies

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.

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:)

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.

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.

commented: Exactly, a lot of people think wrong in Replace method. +8
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.