The following is my search on the Internet, and then I sorted out the section about intercepting the excess part of a specified Chinese Character in. net to replace it with a specified character to expand my mind.
Method 1: Directly operate in the select statement in the background or write a stored procedure in the database
Select substring (field name, 1, 20) from Table Name
Then use the following function in the. aspx file:
<% # Eval ("field name") %> "add your own character here"
In this way, you can implement the select Operation in the database, and then directly read it. You do not need to add code on the page, but this will cause a problem, that is to say, you want to add the title = "<% # Eval (" title ") %> 'in the tag, clicking the title on the webpage does not display all the text, so you must consider your actual situation.
Method 2: Compile a method in the. cs file to intercept characters
////// Cut the specified string by the specified length //////Length to be intercepted///Maximum length of a string///Suffix that exceeds the length///
If the maximum length is exceeded, the system returns the truncated string with the suffix. Otherwise, the system returns the original string.
Public static string StringTruncate (string oldStr, int maxLength, string endWith) {if (string. isNullOrEmpty (oldStr) // throw new NullReferenceException ("the original string cannot be blank"); return oldStr + endWith; if (maxLength <1) throw new Exception ("the returned string length must be greater than [0]"); if (oldStr. length> maxLength) {string strTmp = oldStr. substring (0, maxLength); if (string. isNullOrEmpty (endWith) return strTmp; else return strTmp + endWith;} return oldStr ;}Write the following in the. aspx file:
'> <% # StringTruncate (Eval ("field name"). ToString (), 20, "specified string") %>
Method 3: Use the ternary operator to directly intercept data in aspx.
<%# Eval("title").ToString().Length >30?Eval("title").ToString().Substring(0,20)+"...":Eval("title") %>
This method has not been output to the client, which saves some network resources. At present, there are still some limitations in learning. If you still cannot understand how to save network resources, please enlighten me.
Method 4: Truncate String Length in css
<%# Eval("title") %>
The use of text-overflow: ellipsis has two advantages for overflow text display ellipsis. One is that you do not need to limit the number of words through the program; the other is conducive to SEO. The list of article titles is usually used to display ellipsis in overflow text, which is more friendly to search engines because the title is not actually truncated, but is limited to width but not displayed.
The text-overflow attribute is only an annotation, and whether the omitted mark is displayed when the text overflows. Does not have other style attribute definitions. To achieve the effect of skipping numbers during overflow, you must also define: to force the text to be displayed (white-space: nowrap) in a row and to hide the overflow content (overflow: hidden ), only in this way can the overflow text display effect be achieved.
In fact, no matter which one is used, the core idea is the substring () function, which never changes, and may have mastered the real principle. On this basis, we can choose the desired result from multiple angles.