Get substring of japanese string

advippro

New member
Joined
Nov 18, 2012
Messages
2
Programming Experience
Beginner
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.
 
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.
 
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()
 
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.
 
Back
Top