× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Split a string every 5 characters

Utkarsh Upadhyay
Apr 16, 2015
<p>You can try something like the following:</p> <pre><code>s &lt;- "XOVEWVJIEWNIGOIWENVOIWEWVWEW" # Original string l &lt;- seq(from=5, to=nchar(s), by=5) # Calculate the location where to chop # Add sentinels 0 (beginning of string) and nchar(s) (end of string) # and take substrings. (Thanks to @flodel for the condense expression) mapply(substr, list(s), c(0, l) + 1, c(l, nchar(s))) </code></pre> <p>Output:</p> <pre><code>[1] "XOVEW" "VJIEW" "NIGOI" "WENVO" "IWEWV" "WEW" </code></pre> <p>Now you can <code>paste</code> the resulting vector (with <code>collapse=' '</code>) to obtain a single string with spaces.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/26497583/Split%20a%20string%20every%205%20characters/26497660">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus