Ok, I hope I can explain this so that everybody understands. I have a database where a column has special characters in the fields. I need to remove these characters before printing them to a file. I have the following code:

public string RemoveSpecialChars(string[] args)
    {
        string[] chars = new string[]{",",".","/","!","@","#","$","%","^","&","*","'","\"",";","-","_","(",")",":","|","[","]"," "};
        string[] str = new string[]{};
        
        for(int i = 0; i< chars.Length; i++ )
        {
            if(str.Contains(chars[i]))
                {
                str = str.Replace(chars[i].ToString(),"");
                }
        }
        return str.ToSting();
    }

Then I use the above code like this:

dataType.HL7TypeComponent = RemoveSpecialChars(HL7TypeComponent);

So in the first code I am trying to use an array to collect all the field when it is read from the database. Problem is I do not know how long the array will be because it is coming from the database. First of all I am not sure the code above is correct or if I should even be using an array. Second of all I am getting to errors:
“'System.Array' does not contain a definition for 'Replace' and no extension method 'Replace' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)”
And
“'System.Array' does not contain a definition for 'ToSting' and no extension method 'ToSting' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)”
I am using using System.Linq; so I am not sure what I am missing or what I am doing wrong. Can someone please help? Thank you.

You can only pass in an array of type "char" not string. Change your string[] to char[] and instead of using "" use ''

Also you're creating a new empty string array? Why? You don't even touch the argument you just passed in.

A string array is an array like this:
"hello" "how are you" "I am fine thanks"

It is not an array of characters which is like this:
'h' 'e' 'l' 'l' 'o'

Array does not have a replace method, where-as String does.

Ok so I am still not understanding. In my code, I am using chars to pull in all the characters I need to find in the string that I am pulling in from the database. I know what a string array is, example of the fields I am pulling in from the database that would go into this array are:
Date/timeOfTransaction
PlacerOrder#
PatientIdExternal(externalId)
Timing/quantity
Etc.

I need to take those fields that come in and remove the characters that are in them before putting them in a file. Please clarify what I am doing wrong because I did not understand that response. Thank you.

You can't perform Replace on an array of strings. It has to be a single string object.

Then how would I go about doing what I need to do Ketsuekiame? Please

What about this:

class Program
    {
        static void Main(string[] args)
        {
            string testStr = " 12,23@as&";
            string result = RemoveSpecialChars(testStr);
            Console.WriteLine(result);
            Console.ReadLine();
        }

        public static string RemoveSpecialChars(string s)
        {         
            string[] cleanStr = s.Split(new char[]{'.','&','@',','}); //extend chars as needed
            string newStr= string.Empty;
            for (int i = 0; i < cleanStr.Length; i++)
			{
			    newStr += cleanStr[i];
			}
            return newStr;
        }
    }

ddanbe I tried your code above and I am getting more errors than i was before.

from this line

string[] cleanStr = s.Split(new char[]{",",".","/","!","@","#","$","%","^","&","*","'","\"",";","-","_","(",")",":","|","[","]"," "});

I am geting connot implicitly convert type string to char.

for these lines:

string[] newStr = string.Empty;
newStr += cleanStr[i];
return newStr;

I am getting Cannot implicitly convert type string to string[].

Please help :(

You are defining your char array as a string.
'A' is a char, "A" is a string.
Do you see the difference?
Success!

Ok so I changed that and still getting same errors on the rest of the code, so still noe success.

Did you carefull check everything?
Else post us some code where your errors appear.

I did check everything:

public static string RemoveSpecialChars(string s)
    {
        string[] cleanStr = s.Split(new char[]{',','.','/','!','@','#','$','%','^','&','*',';','-','_','(',')',':','|','[',']',' '});
        string[] newStr = string.Empty;
        
        for(int i = 0; i< cleanStr.Length; i++ )
        {
                newStr += cleanStr[i];
        }
        return newStr;
    }

I get the same error for all three of these lines of code

string.Empty; //gives below error on string
newStr += cleanStr[i]; //gives below error on newStr and cleanStr
return newStr; //gives error below on newStr

gives Cannot implicitly convert type string to string[].

OK, so you did not carefully check everything!;)
Otherwise you would have noticed that I declared newStr as a string, not as a string array!
But I forgive you, I make those mistakes ALL the time.:)

Ok, your right I did not check carefully enough, sorry I looked at it several times. I guess I missed that, my bad. Thank you for forgiving me. So the code builds not but it will not let me use it on a collection of items coming from the database, I get the error Cannot implicitly convert from string collection to string. :(

Is it a string collection you recieve from your DB?

Well yes I am reading through the database via a reader and writing this information to different files and its type is a string collection.

I looked at that before I started posting, it is something like that but I do not know how many items are in the collection because it comes from the database. Items are read into memory from a SQLDataReader, then another method writes this information to file. The items are held in a string collection. So I need to get rid of the bad characters in these fields before they are rendered to the file. So that link does not have what I am looking for. Help!!!!

Ok so I tried something new and its still not working:

public static string RemoveSpecialChars(string[] str)
    {
        string[] chars = new string[]{",",".","/","!","@","#","$","%","^","&","*","'","\"",";","-","_","(",")",":","|","[","]"}; 
        string newStr;
        
        for(int i = 0; i< str.Length; i++ )        
	    {
            for(int j = 0; j< chars.Length; j++)
            {
		        if(str.Contains(chars[j]))            
	            {                
		            newStr = str[i].Replace(chars[j],"");            
	            }
	        }
        }
        
        return newStr;
    }

Getting the error Use of unassigned local variable 'newStr'. So I am assumming that my return is in the wrong spot but I do not understand why. If I move it I get all kinds of other errors. Please let me know what I am doing wrong. Thank you.

Trying something new is always positive!
But I already gave you the solution of your problem in a previous post.
I will repeat it here:

class Program
    {
        static void Main(string[] args)
        {
            string testStr = " 12,23@as&"; 
            string result = RemoveSpecialChars(testStr); 
            Console.WriteLine(result); 
            Console.ReadLine();
        }

        public static string RemoveSpecialChars(string s)
        {
            //string[] chars = new string[] { ",", ".", "/", "!", "@", "#", "$", "%", "^", "&", "*", "'", "\"", ";", "-", "_", "(", ")", ":", "|", "[", "]" };
            //string newStr;

            //for (int i = 0; i < str.Length; i++)
            //{
            //    for (int j = 0; j < chars.Length; j++)
            //    {
            //        if (str.Contains(chars[j]))
            //        {
            //            newStr = str[i].Replace(chars[j], "");
            //        }
            //    }
            //}

            string[] cleanStr = s.Split(new char[] { '.', '&', '@', ',' }); //extend chars as needed    
            string newStr= string.Empty;            
            for (int i = 0; i < cleanStr.Length; i++)			
            {			    
                newStr += cleanStr[i];			
            }            
            return newStr;
        }
    }
}

I you consider your problem solved, please mark it as such.

It did not work as I stated to you because I am looping through an array from a database table. So it did not solve my problem.

So perhaps this will solve it:

class Program
    {
        static void Main(string[] args)
        {
            string[] DBstr = { " 12,23@as&", "6YR$n", "!u!j!&$op" }; // from database
            string[] result = HandleDBstrings(DBstr);
            foreach (string item in result)
            {
                Console.WriteLine(item); 
            }
            Console.ReadLine();
        }

        public static string RemoveSpecialChars(string s)
        {
            string[] cleanStr = s.Split(new char[] { ',', '.', '/', '!', '&', '@', '#', '$' }); //extend chars as needed    
            string newStr= string.Empty;            
            for (int i = 0; i < cleanStr.Length; i++)			
            {			    
                newStr += cleanStr[i];			
            }            
            return newStr;
        }

        public static string[] HandleDBstrings(string[] DBstrs)
        {
            for (int i = 0; i < DBstrs.Length; i++)
            {
                DBstrs[i] = RemoveSpecialChars(DBstrs[i]);
            }
            return DBstrs;
        }

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