regular expressions

J

Jon Paal

when reading a string from a file how to clean off "new line" and "end of line" stuff so text is returned as a clean single line ?
 
T

Tim_Mac

you could just use Trim() on the string which should remove any whitespace
off either end of the line.
but if you want to use regex...

\r is carriage return
\n is newline
a simple replace on these characters with the empty string will sort it out.
System.Text.RegularExpressions.Regex.Replace(input, @"\r\n", "");
hope this helps
tim
 
J

Jon Paal

thanks for the help. It looked like a promising solution but unfortunately did not solve my problem.

I'm trying to read a string from a text file so I can encrypt it.

When I create the text file manually, it reads and encrypts correctly.

If I try to create the text file dynamically, it encrypts to a slightly different outcome.

I can't understand why the difference.

<code>

Private Shared ReadOnly encryptionKeyBytes() As Byte = New Byte() {53, 53, 53, 53, 53, 53, 53, 53}
Private Shared Function GenerateEncryptedLicFile(ByVal inputText As String, ByVal outputFile As String) As Boolean

Dim outputStream As Stream = Nothing
Dim encryptedStream As Stream = Nothing
Dim result As Boolean = False

Dim des As DESCryptoServiceProvider = New DESCryptoServiceProvider()
des.Key = encryptionKeyBytes
des.IV = encryptionKeyBytes

Try
outputStream = New FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)

Dim desEncryptor As ICryptoTransform = des.CreateEncryptor()
encryptedStream = New CryptoStream(outputStream, desEncryptor, CryptoStreamMode.Write)

Dim contents() As Byte = des.EncryptValue(inputText)

encryptedStream.Write(contents, 0, contents.Length)
result = True

Finally
If Not encryptedStream Is Nothing Then
encryptedStream.Close()
encryptedStream = Nothing
End If
If Not outputStream Is Nothing Then
outputStream.Close()
outputStream = Nothing
End If
End Try

Return result
End Function

</code>
 
T

Tim_Mac

hi Jon,
how do you know which is the right one?
can you post your decrypt code.
it could be the \r\n carriage return encoded by notepad if that's how you
created the text file manually.
if you debug, the debugger should show you all the whitespace characters so
you can compare the contents of the file with the string variable, and see
what is the difference.

let me know how you get on. by the way, i can't see where this relates to
the first post, just trying to piece it together!
tim
 
J

Jon Paal

found my problem.

when writing the text file dynamically, I was using objStreamWriter.WriteLine, instead of objStreamWriter.Write, so it was adding a
newline to the entry which I could not strip off.


thanks for responding
 

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

Staff online

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top