<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="213762" data-post-id="213762">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="John-Goff" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/John-Goff/120/19768_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  John-Goff
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Be sure to use <code>String.graphemes/1</code> if there is any possibility of Unicode data in your strings, codepoints can do potentially not what you want. See here for more: <a href="https://hexdocs.pm/elixir/String.html#module-code-points-and-grapheme-cluster" class="inline-onebox" rel="noopener nofollow ugc">String — Elixir v1.20.2</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="213762" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-split-string-into-multiple-chunks-by-size/39662/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-213762" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="213762"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="213764" data-post-id="213764">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="csokun" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/csokun/120/17661_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  csokun
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thank <a class="mention" href="/u/john-goff" rel="nofollow">@John-Goff</a>  I do want to support Unicode string.</p>
<p>Finally, with all your help I managed to put together a working code (decided to go with stream option for now) <a href="https://github.com/csokun/ex_azure_storage/blob/fileshare_create_file/lib/azure_storage/azure_file_share.ex#L177-L189" rel="noopener nofollow ugc">https://github.com/csokun/ex_azure_storage/blob/fileshare_create_file/lib/azure_storage/azure_file_share.ex#L177-L189</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="213764" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-split-string-into-multiple-chunks-by-size/39662/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-213764" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="213764"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="213772" data-post-id="213772">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="hauleth" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/120/18942_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  hauleth
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>What if I would upload file that content is not valid UTF-8? In general in this case you should not care at all about encoding of the file and use <a class="mention" href="/u/cmkarlsson" rel="nofollow">@cmkarlsson</a> solution, but even then you do not need to construct list.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="213772" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-split-string-into-multiple-chunks-by-size/39662/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-213772" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="213772"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="323233" data-post-id="323233">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="axelson" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/axelson/120/26351_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  axelson
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Scenic Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="hauleth" data-post="2" data-topic="39662">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/48/18942_2.png" class="avatar"> hauleth:</div>
<blockquote>
<p><code>for &lt;&lt;chunk::size(chunk_size)-binary &lt;- input&gt;&gt;, do: chunk</code></p>
</blockquote>
</aside>
<p>FYI that snippet will discard the last partial chunk if the string does not divide by <code>chunk_size</code></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex(1)&gt; for &lt;&lt;chunk::size(3)-binary &lt;- "abcdefgh"&gt;&gt; do chunk end
["abc", "def"]
</code></pre>
<p>(if the last partial chunk was included then it would also return <code>"gh"</code>)</p>
<p>Here’s a simple recursive function that will return the last chunk (and not return any empty strings)</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">@doc """
Chunks a string into chunk of a given byte size (NOT unicode safe)

## Examples

    iex&gt; chunk_string("abcdefgh", 3)
    ["abc", "def", "gh"]

    iex&gt; chunk_string("abcdef", 3)
    ["abc", "def"]
"""
def chunk_string(string, chunk_size) when chunk_size &gt; 0 do
  case string do
    &lt;&lt;chunk::size(chunk_size)-binary, rest::binary&gt;&gt; -&gt;
      [chunk | chunk_string(rest, chunk_size)]

    "" -&gt;
      []

    rest -&gt;
      [rest]
  end
end
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="323233" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-split-string-into-multiple-chunks-by-size/39662/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-323233" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="323233"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="323244" data-post-id="323244">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="bdarla" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/bdarla/120/19471_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  bdarla
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>A relevant library that came to my attention recently is <a href="https://github.com/revelrylabs/text_chunker_ex" rel="noopener nofollow ugc">text_chunker_ex</a> by <a href="https://revelry.co/insights/artificial-intelligence/announcing-textchunker/?utm_medium=email&amp;utm_source=elixir-radar" rel="noopener nofollow ugc">Revelry</a> (thanks to <a class="mention" href="/u/hugobarauna" rel="nofollow">@hugobarauna</a> for sharing this in Elixir Radar 420).</p>
<p>The aim of the library is to split text to be fed to AI models. As I understood, it mimics some functionality of the LangChain. I have not used it (yet), but seems interesting!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="323244" data-batch-url="/posts/batch_likers">
                        5
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-split-string-into-multiple-chunks-by-size/39662/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-323244" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="323244"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #15"></div>
  </section>
</div>
    <div class="postbit" id="369775" data-post-id="369775">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="jpc-ae" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jpc-ae/120/38683_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  jpc-ae
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>This is a bit of an old one, but here’s my version that isn’t as memory-friendly as streaming, but it is unicode-safe and better than splitting+joining graphemes. Overall its quite performant:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Regex.split(~r/(.{5000}[\p{Mn}\p{Me}]*|.+)/su, string, include_captures: true, trim: true)
</code></pre>
<p>It’ll return a list of strings chunked into, in this case, 5000 characters (plus any combining marks to ensure they don’t get split up from what they’re attached to), with the remaining bit at the end. This won’t be exactly 5kb depending on how many multi-byte characters you have, but should do fairly well for most purposes. You can always adjust the match string to better reflect the input data structure.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="369775" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-split-string-into-multiple-chunks-by-size/39662/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-369775" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="369775"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>