1 回答

TA貢獻1828條經驗 獲得超4個贊
這是一個 JS,它實現了我在第一條評論中討論的邏輯:
let longAddr = "this is a really long address with lots of words longer than 35 characters all over the place and i dont know where it is going to stop it just keeps going so it's like the longest address in the world which is a little bit crazy but there you go";
let addr = ["","","","",""];
for(let i = 0; i < 5 && longAddr.length > 0; i++){
if(longAddr.length < 35 || i == 4)
{
addr[i] = longAddr;
break;
}
let cut = 35;
while(longAddr[cut] !== " " && cut > 0)
cut--;
if(cut == 0) //word longer than 35 chars here
cut = 35;
addr[i] = longAddr.slice(0, cut);
longAddr = longAddr.slice(cut + 1);
}
console.log(addr);
它會生成一個包含 5 個元素的數組,并將字符串分割成這些元素?,F在您所要做的就是將值放入文本框中
添加回答
舉報