|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
regular expressionswhen 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 ?
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 Show quote "Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message news:%233nIdBG0GHA.1252@TK2MSFTNGP04.phx.gbl... > 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 ? > > > 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> Show quote "Tim_Mac" <tim.mackey@community.nospam> wrote in message news:eZ3ccIZ0GHA.4044@TK2MSFTNGP04.phx.gbl... > 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 > > > > "Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message news:%233nIdBG0GHA.1252@TK2MSFTNGP04.phx.gbl... >> 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 >> ? >> >> >> > > 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 Show quote "Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message news:uXDyVPe0GHA.1252@TK2MSFTNGP04.phx.gbl... > 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> > > > > > > > > > > "Tim_Mac" <tim.mackey@community.nospam> wrote in message > news:eZ3ccIZ0GHA.4044@TK2MSFTNGP04.phx.gbl... >> 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 >> >> >> >> "Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message >> news:%233nIdBG0GHA.1252@TK2MSFTNGP04.phx.gbl... >>> 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 ? >>> >>> >>> >> >> > > 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 Show quote "Tim_Mac" <tim.mackey@community.nospam> wrote in message news:uKnDOie0GHA.1292@TK2MSFTNGP03.phx.gbl... > 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 > > > "Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message news:uXDyVPe0GHA.1252@TK2MSFTNGP04.phx.gbl... >> 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> >> >> >> >> >> >> >> >> >> >> "Tim_Mac" <tim.mackey@community.nospam> wrote in message news:eZ3ccIZ0GHA.4044@TK2MSFTNGP04.phx.gbl... >>> 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 >>> >>> >>> >>> "Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message news:%233nIdBG0GHA.1252@TK2MSFTNGP04.phx.gbl... >>>> 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 ? >>>> >>>> >>>> >>> >>> >> >> > > |
|||||||||||||||||||||||