Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如果超过100,如何在字符串中间插入(…)_Jquery_Html - Fatal编程技术网

Jquery 如果超过100,如何在字符串中间插入(…)

Jquery 如果超过100,如何在字符串中间插入(…),jquery,html,Jquery,Html,我有五个字符串变量,其中两个变量可以随时更改,其余三个变量是常量。现在我想计算五个变量< /强>的长度,如果它超过100,那么在STR1,Str2字符串中,我希望插入(…)在字符串的中间。如果小于100,则不应发生任何事情 注意:再次追加(…)后,五个字符串的总长度不应超过100 //string values can be changed var str1 = "Lorem Ipsum is simply dummy text of the printing and typesetting i

我有五个字符串变量,其中两个变量可以随时更改,其余三个变量是常量。现在我想计算<强>五个变量< /强>的长度,如果它超过100,那么在STR1,Str2字符串中,我希望插入(…)在字符串的中间。如果小于100,则不应发生任何事情 注意:再次追加(…)后,五个字符串的总长度不应超过100

//string values can be changed
var str1 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry";
var str2 = "Lorem Ipsum is simply dummy text of the industry";

//Constant string values
var name1 = "Ramesh";
var name2 = "suresh";
var name3 = "Raj";

可能是这样的:

// this is code what i am having

$(".clickme").click(function(){
       var len = str1.length + str1.length + name1.length + name2.length + name3.length;
        if( len > 100 )
        {
            str1mid = str1.length / 2;
            str2mid = str2.length / 2;
            if(str1mid > 50)
            {
              str1mid = 50;
            }
            if(str2mid > 50)
            {
              str2mid = 50;
            }
            str1 = str1.substring(0,str1mid) + '...' + str1.substring(str1mid,100);
            str2 = str2.substring(0,str2mid) + '...' + str2.substring(str2mid,100);
            console.log(str1);
            console.log(str2);          
        }
        else
        {
            console.log(" Yes it is less then 100");
        }

   alert(len);
});

您可以使用str1.substring(0100)来trunc最大100个字符

我假设您的意思是缩短字符串并添加。。。到最后。这里有一个函数可以做到这一点

if(str1.length > 100)
{
   var middle = str1.length/2;
   str1 = str1.substring(0,middle) + '...' + str1.substring(middle);
}
您可以这样使用它:

function shorten(text) {
  if (text.length > 100) {
    return text.substring(0,97)+"...";
  } else {
    return text;
  }
}

您的问题使您看起来想要一件事,但是您的示例代码看起来想要其他东西。第一个函数是您的问题让它听起来像您想要的,第二个函数是您的示例代码似乎表明的

职能1:

var str1 = shorten("Lorem Ipsum is simply dummy text of the printing and typesetting industry");
函数capLength(str1、str2、name1、name2、name3){
//连接字符串并获得结果长度。
//(这与str1+str2相同……只是稍微快一点。)
var totalLength=[str1,str2,name1,name2,name3]。连接(“”)。长度;
var excessLength=总长度-100;
if(超出长度str1.长度){
str1长度--;
str2Length++;
}
while(str2Length>str2.length){
str2长度--;
str1Length++;
}
str1=str1.子串(0,str1长度-3)+“…”;
str2=str2.子串(0,str2长度-3)+“…”;
返回[str1,str2,name1,name2,name3];
}
职能2:

function capLength(str1, str2, name1, name2, name3) { 
  // Join the strings and get the resulting length.
  // (This is the same as str1 + str2... just marginally faster.)
  var totalLength = [str1, str2, name1, name2, name3].join("").length;

  var excessLength = totalLength - 100;
  if (excessLength <= 0) {
    return; // If the string isn't too long, no changes are needed.
  }

  // Available length for the variable strings (combined) will be
  // 100 - combined length of the constant strings.
  // To get the length of each one, we divide by two.
  var str1Length = (100 - name1.length - name2.length - name3.length) / 2;
  var str2Length = (100 - name1.length - name2.length - name3.length) / 2;

  // It's possible that one string will be under the limit and one will be over.
  // Move the available length from one to the other
  while (str1Length > str1.length) {
    str1Length--;
    str2Length++;
  }
  while (str2Length > str2.length) {
    str2Length--;
    str1Length++;
  }

  str1 = str1.substring(0, str1Length - 3) + "...";
  str2 = str2.substring(0, str2Length - 3) + "...";

  return [str1, str2, name1, name2, name3];
}
函数capLength(str1、str2、name1、name2、name3){
//连接字符串并获得结果长度。
//(这与str1+str2相同……只是稍微快一点。)
var totalLength=[str1,str2,name1,name2,name3]。连接(“”)。长度;
var excessLength=总长度-100;
if(超出长度str1.长度){
str1长度--;
str2Length++;
}
while(str2Length>str2.length){
str2长度--;
str1Length++;
}
var str1Mid=str1.length/2;
var str2Mid=str2.length/2;
如果(str1.length!=str1长度){
str1=(str1.子字符串(0,str1.length/2)+“…”).子字符串(0,str1.length);
}
如果(str2.length!=str2长度){
str1=(str2.子字符串(0,str2.length/2)+“…”).子字符串(0,str2.length);
}
返回[str1,str2,name1,name2,name3];
}

到目前为止,您尝试了什么?您是否有一些代码要共享(即使它目前不工作)?请小心定义这样的函数。如果使用
函数foo(){}
,可以在任何地方调用它。如果使用
var foo=function(){}
,则只能在定义之后调用它。这也将导致字符串长度为103。省略号为长度增加了3个字符。
function capLength(str1, str2, name1, name2, name3) { 
  // Join the strings and get the resulting length.
  // (This is the same as str1 + str2... just marginally faster.)
  var totalLength = [str1, str2, name1, name2, name3].join("").length;

  var excessLength = totalLength - 100;
  if (excessLength <= 0) {
    return; // If the string isn't too long, no changes are needed.
  }

  // Available length for the variable strings (combined) will be
  // 100 - combined length of the constant strings.
  // To get the length of each one, we divide by two.
  var str1Length = (100 - name1.length - name2.length - name3.length) / 2;
  var str2Length = (100 - name1.length - name2.length - name3.length) / 2;

  // It's possible that one string will be under the limit and one will be over.
  // Move the available length from one to the other
  while (str1Length > str1.length) {
    str1Length--;
    str2Length++;
  }
  while (str2Length > str2.length) {
    str2Length--;
    str1Length++;
  }

  var str1Mid = str1.length / 2;
  var str2Mid = str2.length / 2;

  if (str1.length != str1Length) {
    str1 = (str1.substring(0, str1.length / 2) + "...").substring(0, str1Length);
  }
  if (str2.length != str2Length) {
    str1 = (str2.substring(0, str2.length / 2) + "...").substring(0, str2Length);
  }

  return [str1, str2, name1, name2, name3];
}