Results 1 to 4 of 4

Thread: Get substring of japanese string

  1. #1
    advippro is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Nov 2012
    Posts
    2
    Reputation
    0

    Get substring of japanese string

    Hi,

    I'm newbee in VB .net.

    I want to get substring of a japannese string by bytes.
    Example: "住所の明細は添付ファイルをご参考ください"
    I want to get 10 characters from begin: "住所の明細は添付ファ" so number of byte is 20.

    How can i do it???

    Any help will be appreciate.

    Thanks for all your help.

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    12,784
    Reputation
    1620
    You're trying to create a problem where there isn't one. All Strings work the same way in .NET regardless. Even if the text was read from an ASCII file, the String uses two Bytes per Char. If you want the first 10 characters of a String then just call Substring and specify 10 characters.

  3. #3
    advippro is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Nov 2012
    Posts
    2
    Reputation
    0
    Quote Originally Posted by jmcilhinney View Post
    You're trying to create a problem where there isn't one. All Strings work the same way in .NET regardless. Even if the text was read from an ASCII file, the String uses two Bytes per Char. If you want the first 10 characters of a String then just call Substring and specify 10 characters.

    Because in some case it not true, so i do not use substring. And i solve problem like:
    Dim strText As String = "住所の明細は添付ファイルをご参考ください" Dim encText As New System.Text.UTF8Encoding()


    Dim aaa As System.Text.Encoding = System.Text.Encoding.GetEncoding("Shift_JIS")
    Dim bbb() As Byte = aaa.GetBytes(strText)
    Dim ccc As String = aaa.GetString(bbb, 0, 30)
    Console.WriteLine(ccc)
    Console.ReadLine()

  4. #4
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,473
    Reputation
    2732
    The only exception to that rule is when string contains a surrogate pair, a code point represented by two Char values. Char data type has methods to detect this. Shift_JIS has only single/double byte chars and no surrogates.

Bookmarks

Posting Permissions

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