2017-06-15 8 views
0

私は分割したい文字列を持っていました。私はFacebookのメッセンジャーチャットAPIを介してそれを送信する必要があったが、そのAPIは640文字しか許されず、テキストはかなり長くなります。私はテキストを送ることができるきちんとした解決策を求めていました。複数の段落を含む文字列をJavaScriptで2つの半分に分割する

複数の段落を含む弦を一番近いところまで2つに分割するものでした。

例:

var str = "This is paragraph one. I need Mr. Sam to my errand. The errand must be done by him. He is the best. 

Mr. Sharma is also good. Btw this is the second paragraph. I think you get my point. Sentence again. Sentence again. 

Paragraph three is started. I am writing so much now. This is fun. Thanks"; 

//Expected output 

var half1 = "This is paragraph one. I need Mr. Sam to my errand. The errand must be done by him. He is the best. 

Mr. Sharma is also good. Btw this is the second paragraph." 

var half2 = "I think you get my point. Sentence again. Sentence again. 

Paragraph three is started. I am writing so much now. This is fun. Thanks" 
+0

だから640文字を見て、戻ってあなたのように動作... – epascarello

答えて

0
var results=[]; 
var start=0; 
for(var i=640;i<str.length;i+=640){//jump to max 
while(str[i]!=="."&&i) i--;//go back to . 
    if(start===i) throw new Error("impossible str!"); 
    results.push(str.substr(start,i-start));//substr to result 
    start=i+1;//set next start 
} 
} 
//add last one 
results.push(str.substr(start)); 

その後、戻って最後まで行く、前方に640ステップを行うことで、文字列の上に歩くことができます。部分文字列を作成して繰り返します。

+1

上記のコードですか? –

+0

@rohan sood申し訳ありませんtypo ... –

3

は、ベースとしてこれを考えてみましょう:

let slices = [], s; 
for(let i = 0; s = a.slice(i * 640, (i+1) * 640); i++) 
    slices.push(s); 

スライスの配列は、あなたのテキストの640の文字のチャンクが含まれています。しかし、我々はこれが空間認識であることを望んでいる。私たちは、文の終わりを、それを通過することなく、できるだけその640のマークに近づける必要があります。私たちが意識した空間にしたい場合は、それは文全体ではなく、文字に対処する私たちの生活を楽にするよ:ここ

// EDIT: Now, if a sentence is less than 640 chars, it will be stored as a whole in sentences 
// Otherwise, it will be stored in sequential 640-char chunks with the last chunk being up to 640 chars and containing the period. 
// This tweak also fixes the periods being stripped 
let sentences = str.match(/([^.]{0,639}\.|[^.]{0,640})/g) 

はアクションでその厄介な正規表現の簡単なデモです:https://jsfiddle.net/w6dh8x7r

今度は、一度に640文字までの結果を作成できます。

let result = '' 
sentences.forEach(sentence=> { 
    if((result + sentence).length <= 640) result += sentence; 
    else { 
     API.send(result); 
     // EDIT: realized sentence would be skipped so changed '' to sentence 
     result = sentence; 
    } 
}) 
+0

あなたは人生の救済者です:D –

+0

あなたはそれを持っています。私はちょうどあなたが奇妙な行動を得るだろうが、個々の文章の長さが640文字を超えていることを認識したので、心に留めておいてください。 – Will

+0

また、最後の文はどうですか?たとえば、1626文字がある場合などです。最初の1280だけが送信されます。他の人はどうですか? –

0
var txt = 'This is paragraph one. I need Mr. Sam to my errand. The errand must be done by him. He is the best. Mr. Sharma is also good. Btw this is the second paragraph. I think you get my point. Sentence again. Sentence again. Paragraph three is started. I am writing so much now. This is fun. Thanks' 
    var p = [] 
    splitValue(txt, index); 

    function splitValue(text) 
    { 
     var paragraphlength = 40; 
     if (text.length > paragraphlength) 
     { 
      var newtext = text.substring(0, paragraphlength); 
      p.push(newtext) 
      splitValue(text.substring(paragraphlength + 1, text.length)) 
     } 
    } 
関連する問題