C# .NET - Search and Replacing in an Array... - Asked By shah zeb on 10-Oct-11 07:31 AM

Hi,
I have two array
stopWords[] = {"and","the","etc"};
checkIN[]= {"religion","america","and","country","the"};

I need to check in the checkIN[] array the  contents of stopWords array like
if checkIN contains "and", "the" or "etc" then should be replaced by "  replaced by some text "

What are the different options


Regards,
shahzeb
smr replied to shah zeb on 10-Oct-11 07:35 AM
hi

refer this code to replace

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 <= <strong class="highlight">Array</strong>.Length-1) you can do (i < <strong class="highlight">Array</strong>.Length)
    for (int i = 0; i < sArray.Length; i++)
    {
    sArray[4] = sArray[4].Replace(sArray[4].ToString(), lnewvalue.ToString());
    }
  }


refer
http://www.daniweb.com/software-development/csharp/threads/205045
http://www.techpowerup.com/forums/showthread.php?t=74727
Reena Jain replied to shah zeb on 10-Oct-11 07:39 AM
Hi,

try this code and let me know

private void button2_Click(object sender, EventArgs e)
{
string[] sArray = {"religion","america","and","country","the"};
int lnewvalue = "replaced by some text";
 
for (int i = 0; i < sArray.Length; i++)
{
if(sArray[i].ToString()=="and" || sArray[i].ToString()=="the" || sArray[i].ToString()=="etc")
sArray[i] = sArray[i].Replace(sArray[i].ToString(), lnewvalue.ToString());
}
}

this will replace the string with desire string
Web Star replied to shah zeb on 10-Oct-11 07:42 AM
you need to use two loop for as nested loop and than replace it

stopWords[] = {"and","the","etc"};
checkIN[]= {"religion","america","and","country","the"};

for(int i=0; i<=stopWords.Length; i++)
{
for(int j=0; i<=checkIN.Length; j++)
{
if(checkIN[j].ToString().IndexOf(stopWords[i].ToString() > 0)
{
//here found that content of stopword array in checkIN array so you need to replace with another text //what you want as follows
checkIN[j] = "new text here";
}
}

}
TSN ... replied to shah zeb on 10-Oct-11 07:43 AM
hi..try like this...



        string[] stopWords = { "and", "the", "etc" };
        string[] checkIN = { "religion", "america", "and", "country", "the" };
        foreach (string s in stopWords)
        {
          for (int i = 0; i < checkIN.Count(); i++)
          {
            if (checkIN[i].ToString() == s)
            {
              checkIN[i].Replace(checkIN[i], "ChangedString");
            }
          }
        }
dipa ahuja replied to shah zeb on 10-Oct-11 03:01 PM
Try this way:
string[] stopWords = { "and""the""etc" };
string[] checkIN = { "religion""america""and""country""the" };
 
foreach (string s in stopWords)
{
  if (checkIN.Contains(s))
  {
    Response.Write("<br>It contains :" + s);
  }
}
aneesa replied to shah zeb on 11-Oct-11 04:31 AM
Try the code Below
string[] stopWords = { "and", "the", "etc" };
string[] checkIN = { "religion", "america", "and", "country", "the" };
for (int i = 0; i < checkIN.Length; i++)
{
  foreach (string stop in stopWords)
  {
    if (checkIN[i].Contains(stop))
    {
      checkIN[i]=  checkIN[i].Replace(stop, "the new value");
    }
  }
}