当前位置:网站首页>Limit the input character length to 1 character in English and 2 characters in Chinese
Limit the input character length to 1 character in English and 2 characters in Chinese
2022-06-26 02:43:00 【xyphf_ HEPAI Kongming】
js Although it provides a function to calculate the number of bytes occupied by a string , However, the number of bytes occupied by Chinese characters cannot be calculated correctly , Such as document.form1.username.value=“ Hello, Shanghai ”,document.form1.username.value.length The return is 5 instead of 10, So it brings some trouble to programming , Such as restricting the input of fixed length characters , Intercepting fixed length strings can not achieve the desired effect because of this problem , The following is a simple function written by myself , Used to intercept a fixed length string , Both Chinese and English are applicable .
// js Intercepting string , Both Chinese and English can be used
// If the given string is greater than the specified length , Intercepts the specified length and returns , If not, return the source string .
cutstr(str,len) {
var str_length = 0;
var str_len = 0;
var str_cut = new String();
var str_len = str.length;
for(var i = 0;i<str_len;i++)
{
var a = str.charAt(i);
str_length++;
if(escape(a).length > 4)
{
// The length of Chinese characters after encoding is greater than 4
str_length++;
}
str_cut = str_cut.concat(a);
if(str_length>=len)
{
str_cut = str_cut.concat("...");
return str_cut;
}
}
// If the given string is less than the specified length , Then return the source string ;
if(str_length<len){
return str;
}
},Such as cutstr(" Hello, Shanghai ",6); What will be returned will be : How do you do ...
边栏推荐
- Matlab|基于BP神经网络进行电力系统短期负荷预测
- What happens when the cloud answer does not display the third-party login button
- Cox 回归模型
- Gold three silver four~
- Possible values for @supply in kotlin
- Implement decorator pattern for servicecollection
- 用指南针交易股票安全吗?指南针是如何交易股票的,需要开户吗
- Consumer of microservices
- Oracle练习
- WPF window centering & change trigger mechanism
猜你喜欢

【机器学习】基于多元时间序列对高考预测分析案例

基於鄰接矩陣的廣度優先遍曆

Here comes the official zero foundation introduction jetpack compose Chinese course!

@Query difficult and miscellaneous diseases

Redis Lua sandbox bypass command execution (cve-2022-0543)

2021-08-04

奶牛排序问题

How do I fix the iPhone green screen problem? Try these solutions

Cow sequencing problem

Possible values for @supply in kotlin
随机推荐
How to solve the problem that the iPhone 13 lock screen cannot receive the wechat notification prompt?
7 tips to change your life
2022-06-25:给定一个正数n, 表示有0~n-1号任务, 给定一个长度为n的数组time,time[i]表示i号任务做完的时间, 给定一个二维数组matrix, matrix[j] = {a,
Dynamic analysis and calculation of expression, flee is really sweet to use
基於鄰接矩陣的廣度優先遍曆
What is the sudden power failure of iPhone and how to solve it?
OpenAPI 3.0 specification - Food Guide
分享一些在内网操作的远程办公经验| 社区征文
Audio video and CPU architecture
Install development cross process communication
如何在 ggplot2 中向绘图中添加表格
Is it safe to open a securities account at the school of Finance and business in 2022?
官方零基础入门 Jetpack Compose 的中文课程来啦!
表达式的动态解析和计算,Flee用起来真香
[image filtering] image filtering system based on Matlab GUI [including Matlab source code 1913]
Dreamcamera2 video recording, playing without sound, recording function is normal, using a third-party application for video recording, playing with sound
The "more" option of Spreadtrum platform dearmcamera2 is removed
网上股票开户安全吗?
df报错Stale file handle
图的广度优先遍历