Home > Net >  C # c # string length resolution not garbled words according to the rules
C # c # string length resolution not garbled words according to the rules

Time:10-14

Do today winform project run into a problem when I print, please god to give directions,
A set of string, there are Numbers, Chinese character, Chinese punctuation, letters, when I was in print, according to the need to split into multiple lines of isometric
Use the following code query data, but encounter characters sometimes garbled words,

Public String [] getstr (String STRS, int len)
{
Double I=STRS. Length;
String [] myarray on=new string [int. Parse (Math) between (I/len). The ToString ()));
For (int j=0; J & lt; Myarray.length; J++)
{
Len=len & lt;=STRS. Length? Len: STRS. Length;
[j]=myarray on STRS. Substring (0, len);
STRS=STRS. Substring (len, STRS. Length - len);
}
Return myarray on;
}

Here I have a special request, the first line shows the length of the line with other different, other line shows are the same length, the first line can be according to the parameter specifies the length, please help! Thank you very much!

CodePudding user response:

In the Chinese characters in.net is one character at a time, and will not be separated, how can display the code

CodePudding user response:



Effect is like this

CodePudding user response:

Input is garbled, output is stil

You people use UTF8, for example, you use GBK

Of course, you in this code, the so-called isometric shows itself problematic

12345
The 12345


See yao, are five characters, they were?

CodePudding user response:

Sorry, just code errors, I use the
Public String [] getstr (String STRS, int onelen, int len)
{
Byte [] subbyte=System. Text. Encoding. The Default. The GetBytes (STRS);

Double I=subbyte. Length;
String [] myarray on=new string [int. Parse (Math) between (I/len). The ToString ()));

For (int j=1; J & lt; Myarray.length; J++)
{
Len=len & lt;=subbyte. Length? Len: subbyte. Length;
Myarray on [j]=System. Text. Encoding. The Default. Get string (subbyte, 0, len);
STRS=System. Text. Encoding. The Default. Get string (subbyte, len, subbyte Length - len);
Subbyte=System. Text. Encoding. The Default. GetBytes (STRS);
}

Return myarray on;
}

CodePudding user response:

Finally this should not be more, in a Chinese character is multi-byte, you want to break up byte less likely,

Label he can wrap himself

CodePudding user response:

If you want to manually print branch
You demand than you actually get this code is complex
Because most of print font is not monospaced font, the second code doesn't take into account not only after the byte stream segmentation characters may be truncated problem, also have never thought character itself width is different
To truly achieve manually branch, so need specific Graphics to display/print device to MeasureString get real width
By analyzing characters whether need to be assigned to the next line

CodePudding user response:

System. Text. Encoding. The Default error,

CodePudding user response:

 Graphics Graphics=CreateGraphics (); 
SizeF SizeF=graphics. MeasureString (" aaaaaaaaa ", the new Font (9) "song typeface,");
MessageBox. Show (the string. Format (" font Width: {0}, Height: {1} ", sizeF. Width, sizeF, Height));


Have a try
  •  Tags:  
  • C#
  • Related