- Home /
Unity C#: removing special characters from a string
Am reading a text file that contains words, numbers and special characters, I want to remove certain special characters like: [](),'
I have this code but it is not working !
using (var reader = new StreamReader ("C://Users//HP//Documents//result2.txt")) {
string line = reader.ReadToEnd ();
Regex rgx = new Regex ("[^[]()',]");
string res = rgx.Replace (line, "");
Message1.text = res;
}
what am I missing, thanks
Comment
Answer by KittenSnipes · Jan 21, 2018 at 02:36 AM
Well this deletes the characters and writes it in to a new file. So here you go:
public char[] CharactersToRemove;
void Start () {
char ch;
int Tchar = 0;
string directoryLoc = "C://Users//HP//Documents//";
StreamReader reader;
File.WriteAllText(directoryLoc + "NewResult2.txt", string.Empty);
reader = new StreamReader(directoryLoc + "result2.txt");
do
{
ch = (char)reader.Read();
for (int i = 0; i < CharactersToRemove.Length; i++)
{
if (ch.Equals(CharactersToRemove[i]))
{
ch = '\0';
}
}
using (StreamWriter newWriter = new StreamWriter(directoryLoc + "NewResult2.txt", true))
{
newWriter.Write(ch);
}
Tchar++;
} while (!reader.EndOfStream);
Debug.Log("Document Cleaning Done!");
reader.Close();
reader.Dispose();
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to make buttons have sound when it is highlighted and clicked? 0 Answers
Generate a new ball when the First Ball is release. 0 Answers
Networking problems on connection 0 Answers