2016-04-06 54 views
0
//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"; 

我有五個字符串變量,可以隨時更改兩個變量,其餘三個變量都是常量。現在我想計算五個變量的長度,如果它超過100,那麼在str1中,str2字符串我想插入(...)在字符串中間。如果是小於100沒有什麼應該發生 注:追加(...)再次五串的總長度後應該不會超過100如何在字符串的中間插入(...)如果它超過100

// 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); 
}); 
+5

你嘗試過這麼遠嗎?你有一些代碼可以共享(即使它目前不工作)? –

回答

0

也許這樣的事情:

if(str1.length > 100) 
{ 
    var middle = str1.length/2; 
    str1 = str1.substring(0,middle) + '...' + str1.substring(middle); 
} 

您可以使用str1.substring(0,100)截斷您最多100個字符

0

我假定您的意思是縮短字符串並將...添加到最後。這是一個可以做到的功能。

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

您可以使用它像這樣:

var str1 = shorten("Lorem Ipsum is simply dummy text of the printing and typesetting industry"); 
+0

要小心定義這樣的功能。如果你使用'function foo(){}',你可以在任何地方調用它。如果你使用'var foo = function(){}',你只能在定義之後調用它。 – 3ocene

+0

此外,這將導致一個長度爲103的字符串。省略號將3個字符添加到長度。 – 3ocene

+1

感謝伯爾尼,我編輯了它。 –

0

你的問題使得它看起來像你想的一件事,但是你的示例代碼看起來像你想要別的東西。第一個功能是你的問題使得它聽起來像你想要的,第二個是你的示例代碼似乎表明了什麼。

功能1:

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]; 
} 

功能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++; 
    } 

    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]; 
} 
相關問題