Results 1 to 14 of 14

Thread: Special characters in string text

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    18

    Special characters in string text

    I'm working on a form in which strings of text are being imported.
    A lot of these strings contain special characters, like é, ö, ü, &, ï, etc...

    When I convert these strings to text, so they can be shown as labels, the special characters either disappear (like the '&' sign) or they are shown as some sort of icons.
    How can I let VB handle these strings, so the special characters are shown the way they should, without having to change the (many hundreds) original strings of text?

    Thanks for your help!

  2. #2
    Hyperactive Member marniel647's Avatar
    Join Date
    Aug 2010
    Location
    MSDN Library
    Posts
    259

    Re: Special characters in string text

    Quote Originally Posted by Ivo77 View Post
    I'm working on a form in which strings of text are being imported.
    A lot of these strings contain special characters, like é, ö, ü, &, ï, etc...

    When I convert these strings to text, so they can be shown as labels, the special characters either disappear (like the '&' sign) or they are shown as some sort of icons.
    How can I let VB handle these strings, so the special characters are shown the way they should, without having to change the (many hundreds) original strings of text?

    Thanks for your help!
    i think you can shown a string in a label.. string and text is almost the same so you dont need to convert the string into text...

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    18

    Re: Special characters in string text

    Well, what I mean is, for instance:

    text file contains "This is an éxample text & this is anöther line."
    I import this line of text as a string (String1)
    Then I want to show the text in a label:
    Label1.text = String1
    When I run the form, Label1 shows: "This is an *xample text this is an*ther line", where the * are sort of icons in stead of é and ö, and the &-sign disappears.

  4. #4
    Hyperactive Member marniel647's Avatar
    Join Date
    Aug 2010
    Location
    MSDN Library
    Posts
    259

    Re: Special characters in string text

    lol i try also the text and it is working.. ?

    screen shot..


  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    18

    Re: Special characters in string text

    Quote Originally Posted by marniel647 View Post
    lol i try also the text and it is working.. ?
    Thanks for the test,
    however, this is not what I mean.
    You typed the text directly into the textbox code. But in my case, I have to import the text from .TXT or .CSV files:

    Dim Import As New System.IO.StreamReader("C:\file.txt")
    Textstring = Import.ReadLine()
    Label1.text = Textstring


    This is where I'm unable to keep the special characters the way they are in the TXT file.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Special characters in string text

    You will presumably need to specify the encoding when creating the StreamReader. You could try Encoding.UTF7 and .Unicode first and then try others if they don't work.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    18

    Re: Special characters in string text

    Quote Originally Posted by jmcilhinney View Post
    You will presumably need to specify the encoding when creating the StreamReader. You could try Encoding.UTF7 and .Unicode first and then try others if they don't work.
    Thanks for the suggestion!
    Could you help me out a little? I've never specified encoding before. Can you give me an example on how to do this?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Special characters in string text

    Check out the documentation for the StreamReader class. Examples provided there.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    18

    Re: Special characters in string text

    Quote Originally Posted by jmcilhinney View Post
    Check out the documentation for the StreamReader class. Examples provided there.
    Thanks,

    I've managed to get the é, ö, ü, etc. characters imported correctly by using:

    Dim Importstring As New System.IO.StreamReader("C:\File.txt", System.Text.Encoding.GetEncoding(1252))

    However, I still haven't figured out how to prevent the ampersand (&) character from disappearing when I import a string of text and put it into a label.
    With the code above I get the following result:
    Text in file =

    "Thïs is ä liné of têxt & this is anöther one."

    Text after importing with streamreader, using system.text.encoding.getencoding(1252):

    "Thïs is ä liné of têxt this is anöther one."

    So, the '&' (ampersand sign) keeps disappearing.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Special characters in string text

    That "issue" with the ampersand is by design. That's how you create an accelerator key. For instance, let's say that you have a TextBox for the user's first name and you have a Label before it with the text "First Name:". If you actually set the Text of the Label to "&First Name:" then the ampersand is not displayed and the "F" character will be underlined. That indicates that the user can press Alt+F to set focus to that TextBox. If you want an ampersand to appear in such cases, you need to escape it with another ampersand, e.g. "This && That".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Special characters in string text

    Quote Originally Posted by jmcilhinney View Post
    If you want an ampersand to appear in such cases, you need to escape it with another ampersand, e.g. "This && That".
    Or you could just set the controls UseMnemonic Property to False.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    18

    Re: Special characters in string text

    Quote Originally Posted by Edgemeal View Post
    Or you could just set the controls UseMnemonic Property to False.
    Thanks so much, that's what I was looking for!

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    18

    Re: Special characters in string text

    Now I'm facing a new, related problem.

    This all works fine as long as I'm using streamreader to import text.
    However, if I try to write this text to a CSV file by using streamwriter, the special characters like é, ü, ö, etc. get all scrambled again.

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    18

    Re: Special characters in string text

    Quote Originally Posted by Ivo77 View Post
    Now I'm facing a new, related problem.

    This all works fine as long as I'm using streamreader to import text.
    However, if I try to write this text to a CSV file by using streamwriter, the special characters like é, ü, ö, etc. get all scrambled again.
    Never mind, I asked for help too quickly. I could use the System.Text.Encoding.GetEncoding(1252) code for streamwriter as well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width