How to write to file " "

M

Mrozu

Hi
When I try write to file(with StreamWriter) some string, i have some
problem. I can't write text with "". For example i want to wrote
"Hello" . When i make

Dim sw As New System.IO.StreamWriter("C:\text.txt")

sw.write("Hello")

..
..
..
..

In my file is Hello, instead of "Hello". How i can write " ??

thx Mrozu
 
T

The Grim Reaper

The quotation mark is represented by a character - ASCII number 34.
You can create a character by using Convert.ToChar(34), like so;

Dim sw As New System.IO.StreamWriter("C:\TextFile.txt")

sw.Write(Convert.ToChar(34) & "Hello" & Convert.ToChar(34))


I'm sure the experts in this group can suggest better methods...

______________________________________
The Grim Reaper
 
H

Herfried K. Wagner [MVP]

Mrozu said:
When I try write to file(with StreamWriter) some string, i have some
problem. I can't write text with "". For example i want to wrote
"Hello" . When i make

Dim sw As New System.IO.StreamWriter("C:\text.txt")

sw.write("Hello")

In my file is Hello, instead of "Hello". How i can write " ??

'sw.Write("""Hello""")'.
 
W

webmaster

You could also do this:

Write("""My text""")


If you put two quotation marks in the place of one inside the text, that does the trick.

**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
 
T

The Grim Reaper

Personal preference - to me that makes code very unreadable!

Especially with ASP scripts.

__________________________________
The Grim Reaper
 
M

Mrozu

Thx everyone. all mehods work. But why """(3x"). i were trying
""""HELLO"""" but it doesn't work. I can't understand why 3:) But ok,
it works, and it is the most important:)

Thx Mrozu
 
H

Herfried K. Wagner [MVP]

Mrozu said:
Thx everyone. all mehods work. But why """(3x"). i were trying
""""HELLO"""" but it doesn't work. I can't understand why 3:) But ok,
it works, and it is the most important:)


The two outer quotation marks are used to delimit the string literal. Each
quotation mark inside the string literal is encoded by two consecutive quote
characters.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top