bargavik93
bargavik93

Reputation: 27

how to replace some text and again assign it to same string in c#

In my application I will get the file name with -b. I need to replace the -b and after replacing it I need to again add that filename to the doc. How can I do this?

The filename in doc is 123-b.docx, I need to replace the -b, then after replacment I should get 123.doc this should be given to doc.

if (ddlstype.SelectedValue == "1")
{
    doc = filup1.FileName;
    if (doc.ToLower().Replace("-b", "").ToString())
    {

    }

    ReadFiles(doc1, doc2, ""); 
}
else if (ddlstype.SelectedValue == "2")
{   
    ReadFiles(doc1, "" , ""); 
}
else if (ddlstype.SelectedValue == "3")
{       
    ReadFiles(doc1,"", doc2); 
}

I am getting this error:

Cannot implicitly convert type string to bool near:
if (doc.ToLower().Replace("-b", "").ToString())

Upvotes: 0

Views: 90

Answers (2)

Yair Halberstadt
Yair Halberstadt

Reputation: 6841

ToString() returns a string and you are using it in an if statement. That is why you are getting a compile error. An if statement requires something which is either true or false inside eg:

if (a==4)
{ 
  ...

ToString() does not return something true or false. It returns a string.

What are you trying to check for in your if statement?

Upvotes: 1

Willy David Jr
Willy David Jr

Reputation: 9151

Try this:

doc = doc.ToLower().Replace("-b", "");

Remove the if. So it would be:

if (ddlstype.SelectedValue == "1")
{
    doc = filup1.FileName;
    doc = doc.ToLower().Replace("-b", "");

    ReadFiles(doc1, doc2, ""); 
}

I remove if since if statement are selection statements, which means it checks your statement if its true or false. So declaring the value of the variable doc should not be on if statements.

Upvotes: 0

Related Questions