ArticleDirectory
String S = "Iam gun ";
Int Len = S. length; // will output as 6
Byte [] Sarr = system. Text. encoding. Default. getbytes (s );
Len = Sarr. length; // will output as 3 + 3*2 = 9
Public Static String Getfirststring ( String Stringtosub, Int Length)
{
RegEx = New RegEx ( " [\ U4e00-\ u9fa5] + " , Regexoptions. Compiled );
Char [] Stringchar = Stringtosub. tochararray ();
Stringbuilder sb = New Stringbuilder ();
Int Nlength = 0 ;
Bool Iscut = False ;
For ( Int I = 0 ; I < Stringchar. length; I ++ )
{
If (RegEx. ismatch (stringchar [I]). tostring ()))
{
SB. append (stringchar [I]);
Nlength+ = 2;
}
Else
{
SB. append (stringchar [I]);
Nlength=Nlength+ 1;
}
If (Nlength > Length)
{
Iscut=True;
Break;
}
}
If (Iscut)
Return SB. tostring () + " .. " ;
Else
Return SB. tostring ();
}
I found the method from the Internet and added it. If the length is exceeded, add the ".." character later.
This is the most useful way to display news titles when you create a website. To maintain the page pattern, you need to limit the title length, which requires two-character Chinese characters.
Feedback # Re: C # returns a response using a string of the specified length in Chinese and English
By garbage pig good, you can also not use a regular expression
Convert each word into byte []
If a byte has a high or low position, there are two locations.
In this way, it also adapts to the characters in the other two locations (other languages) # Re: C # intercept the specified length of Chinese and English strings to reply
By baojian · 123 iron and steel · 123steel. cn SS using this method can also control the display:
======================================
<Style>
# Iddiv {width: 100%; Height: 90px; Background-color: #87 ceeb; padding: 8px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap ;}
# Idcodediv {width: 100%; padding: 4px; font-family: verdana, tahoma; margin: 12px 0px 0px 0px; Background-color: # eeeeee; font-weight: bold ;}
</Style>
<Div id = iddiv> select the attribute value of the text in the selection box below. Take a look at what will happen, and then you will understand what they mean. </Div>
<Br>
<Table> <tr> <TD>
<Select id = "idsel1" onchange = "rdl_change ();">
<Option value = "hidden"> --- overflow ---
<Option value = "hidden"> hidden
<Option value = "visible"> visible
</SELECT>
</TD> <TD>
<Select id = "idsel2" onchange = "rdl_change ();">
<Option value = "clip"> --- text-overflow ---
<Option value = "clip"> clip
<Option value = "ellipsis" selected> ellipsis
</SELECT>
</TD> </tr> </table>
<Div id = idcodediv> overflow: ellipsis; <br> text-overflow: hidden; </div> # Re: C # Return a response using a string of a specified length in Chinese and English
By XiaohuiCodeI think it's very familiar...
Http://xiaohui.cnblogs.com/archive/2004/10/23/55868.html
In the comments, he pointed out that there are more Chinese character sets.
# Re: C # Return by intercepting a string of a specified length in Chinese and English
By dibao I searched my name from Google and came back to see it again. Now my new method is:
Encoding encoding = encoding. getencoding ("gb2312 ");
// This method is better for operating systems in different languages, and other character sets are also easy to modify;
// If it is a Chinese OS, you can also use encoding. Default instead.
Int bytessize = encoding. getbyte ("percyboy"). length;
// The expression above indicates the number of bytes of the string in the gb2312 character set.
// Use this to correct your code and perform a test,
// I guess it should be faster than the regular expression. # Re: C # Return by intercepting a string of a specified length in Chinese and English
By laifangsong getfirststring ("AB me", 3)
The output is "AB" and I don't seem to want it.
It feels better.
Public static string getfirststring (string stringtosub, int length)
{
RegEx = new RegEx ("[\ u4e00-\ u9fa5] +", regexoptions. Compiled );
Char [] stringchar = stringtosub. tochararray ();
Stringbuilder sb = new stringbuilder ();
Int nlength = 0;
For (INT I = 0; I <stringchar. length; I ++)
{
If (RegEx. ismatch (stringchar [I]). tostring ()))
{
Nlength + = 2;
}
Else
{
Nlength = nlength + 1;
}
If (nlength <= length)
{
SB. append (stringchar [I]);
}
Else
{
Break;
}
}
If (sb. tostring ()! = Stringtosub)
{
SB. append ("...");
}
Return sb. tostring ();
}