0

I'm making a small C# app and I have a small issue .

I have a .xml with plain text and I need only the 4th line .

string filename = "file.xml";
if (File.Exists(filename))
{
    string[] lines = File.ReadAllLines(filename);
    textBox1.Text += (lines[4]);
}

Till now everything is good, my only problem is that I have to remove from the 4th line some words and symbols .

My bad words and symbols :

word 1 
: 
' 
, 

I'v been looking on google however I couldn't find anything for C#. Found a code for VB , but I'm new in this and I really don't know how to convert it and make it working.

 Dim crlf$, badChars$, badChars2$, i, tt$
  crlf$ = Chr(13) & Chr(10)
  badChars$ = "\/:*?""<>|"           ' For Testing, no spaces
  badChars2$ = "\ / : * ? "" < > |"  ' For Display, has spaces

  ' Check for bad characters
For i = 1 To Len(tt$)
  If InStr(badChars$, Mid(tt$, i, 1)) <> 0 Then
    temp = MsgBox("A directory name may not contain any of the following" _
           & crlf$ & crlf$ & "     " & badChars2$, _
           vbOKOnly + vbCritical, _
           "Bad Characters")
    Exit Sub
  End If
Next i

Thank you.

FIXED :)

 textBox1.Text += (lines[4]
              .Replace("Word 1", String.Empty)
            .Replace(":", String.Empty)
            .Replace("'", String.Empty)
            .Replace(",", String.Empty));
3
  • 1
    If your file is XML, you really, really should parse it as XML. Try XDocument.Load.
    – driis
    Commented Mar 24, 2013 at 10:22
  • Checking string.Replace , Thanks. --- The content for the XML is taken from WEB , a JS script .
    – rgerculy
    Commented Mar 24, 2013 at 10:23
  • 1
    You should accept the answer if it helped, not just mark the question as fixed... (it will give you 5 rep as well :) )
    – antonijn
    Commented Mar 24, 2013 at 10:29

3 Answers 3

2

You could replace them with nothing:

textBox1.Text += lines[4].Replace("word 1 ", string.Empty)
                         .Replace(":", string.Empty)
                         .Replace("'", string.Empty)
                         .Replace(",", string.Empty);

Or perhaps create an array of expressions you want removed, and replace them all with nothing.

string[] wordsToBeRemoved = { "word 1", ":", "'", "," };

string result = lines[4];
foreach (string toBeRemoved in wordsToBeRemoved) {
    result = result.Replace(toBeRemoved, string.Empty);
}
textBox1.Text += result;
2
  • Is there any way to remove space as well ?
    – rgerculy
    Commented Mar 24, 2013 at 10:44
  • @rgerculy Sure, just add " " to wordsToBeRemoved.
    – antonijn
    Commented Mar 24, 2013 at 10:45
1

You can use String.Replace to replace them with nothing:

textBox1.Text += (lines[4]
            .Replace("Word 1", String.Empty)
            .Replace(":", String.Empty)
            .Replace("'", String.Empty)
            .Replace(",", String.Empty));
0
0

Guys gave good solutions, I just want to add another fast (using StringBuilder) and convenient (using Extension method syntax and params as values) solution

public static string RemoveStrings(this string str, params string[] strsToRemove)
{
    var builder = new StringBuilder(str);
    strsToRemove.ToList().ForEach(v => builder.Replace(v, ""));
    return builder.ToString();
}

now you can

string[] lines = File.ReadAllLines(filename);
textBox1.Text += lines[4].RemoveStrings("word 1", ":", "'", ",");
4
  • Thanks for your answer , but I want something really simple to can understand my self too :)
    – rgerculy
    Commented Mar 24, 2013 at 10:56
  • @rgerculy there are 3 short lines of code in method implementation, how simpler can you get? Commented Mar 24, 2013 at 11:02
  • True ... but the second line " strsToRemove.ToList().ForEach(v => builder.Replace(v, "")); " is confusing me . I'm new, and I have to learn many things .
    – rgerculy
    Commented Mar 24, 2013 at 11:15
  • 1
    ok, I understand, there will be time, when you will grasp all the power of LINQ, my son. Let the power of Common Language Runtime be with you, young padavan. Commented Mar 24, 2013 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.