snipt

Ctrl+h for KB shortcuts

C#

Length in Bytes

1
2
3
4
5
6
 void Main()
{
	byte [] bytes = System.Text.Encoding.ASCII.GetBytes("123456789123456789");
	
	bytes.Length.Dump();
}
https://snipt.net/embed/8f2609e797a2ea4d380f245a87fa58b9/
https://snipt.net/raw/8f2609e797a2ea4d380f245a87fa58b9/
8f2609e797a2ea4d380f245a87fa58b9
csharp
C#
7
2016-09-22T19:47:46
True
False
False
Oct 16, 2013 at 07:57 PM
/api/public/snipt/88200/
length-in-bytes
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a> <span class="k">void</span> <span class="nf">Main</span><span class="p">()</span> </span><span id="L-2"><a name="L-2"></a><span class="p">{</span> </span><span id="L-3"><a name="L-3"></a> <span class="kt">byte</span> <span class="p">[]</span> <span class="n">bytes</span> <span class="p">=</span> <span class="n">System</span><span class="p">.</span><span class="n">Text</span><span class="p">.</span><span class="n">Encoding</span><span class="p">.</span><span class="n">ASCII</span><span class="p">.</span><span class="n">GetBytes</span><span class="p">(</span><span class="s">&quot;123456789123456789&quot;</span><span class="p">);</span> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a> <span class="n">bytes</span><span class="p">.</span><span class="n">Length</span><span class="p">.</span><span class="n">Dump</span><span class="p">();</span> </span><span id="L-6"><a name="L-6"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
C#.Bytes, length

JavaScript

Limit string length/trim text with jQuery

1
2
3
4
5
6
7
8
jQuery(document).ready(function($){
	$("h3").each(function(){
	        if ($(this).text().length > 30) {
	            $(this).text($(this).text().substr(0, 27)); 
	            $(this).append('...');
	        }
	    });
});
https://snipt.net/embed/cff52cef5e9cd507cea011f043282c54/
https://snipt.net/raw/cff52cef5e9cd507cea011f043282c54/
cff52cef5e9cd507cea011f043282c54
js
JavaScript
8
2016-09-30T15:59:57
True
False
False
/api/public/snipt/43488/
limit-string-lengthtrim-text-with-jquery
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="nx">jQuery</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">ready</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">$</span><span class="p">){</span> </span><span id="L-2"><a name="L-2"></a> <span class="nx">$</span><span class="p">(</span><span class="s2">&quot;h3&quot;</span><span class="p">).</span><span class="nx">each</span><span class="p">(</span><span class="kd">function</span><span class="p">(){</span> </span><span id="L-3"><a name="L-3"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">text</span><span class="p">().</span><span class="nx">length</span> <span class="o">&gt;</span> <span class="mi">30</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-4"><a name="L-4"></a> <span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">text</span><span class="p">(</span><span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">text</span><span class="p">().</span><span class="nx">substr</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">27</span><span class="p">));</span> </span><span id="L-5"><a name="L-5"></a> <span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">append</span><span class="p">(</span><span class="s1">&#39;...&#39;</span><span class="p">);</span> </span><span id="L-6"><a name="L-6"></a> <span class="p">}</span> </span><span id="L-7"><a name="L-7"></a> <span class="p">});</span> </span><span id="L-8"><a name="L-8"></a><span class="p">});</span> </span></pre></div> </td></tr></table>
javascript, jquery, length, limit, trim

JavaScript

Trim string to length

1
2
3
4
5
function trimLength(str, length) {
	str = $.trim(str); // jQuery required
	str = str.slice(0, length);
	return str;
}
https://snipt.net/embed/b282ce90eccedb80413e42bfea8f0572/
https://snipt.net/raw/b282ce90eccedb80413e42bfea8f0572/
b282ce90eccedb80413e42bfea8f0572
js
JavaScript
5
2016-09-30T14:01:42
True
False
False
/api/public/snipt/23868/
trim-string-to-length
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="kd">function</span> <span class="nx">trimLength</span><span class="p">(</span><span class="nx">str</span><span class="p">,</span> <span class="nx">length</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-2"><a name="L-2"></a> <span class="nx">str</span> <span class="o">=</span> <span class="nx">$</span><span class="p">.</span><span class="nx">trim</span><span class="p">(</span><span class="nx">str</span><span class="p">);</span> <span class="c1">// jQuery required</span> </span><span id="L-3"><a name="L-3"></a> <span class="nx">str</span> <span class="o">=</span> <span class="nx">str</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nx">length</span><span class="p">);</span> </span><span id="L-4"><a name="L-4"></a> <span class="k">return</span> <span class="nx">str</span><span class="p">;</span> </span><span id="L-5"><a name="L-5"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
cut, function, helper, javascript, length, shorten, snippet, string, trim

PHP

Excerpt length in WordPress

1
2
3
4
5
6
<?php
function new_excerpt_length($length) {
	return 15;
}
add_filter('excerpt_length', 'new_excerpt_length');
?>
https://snipt.net/embed/b302a9e0a30eaac75c25a8b4b161bcb6/
https://snipt.net/raw/b302a9e0a30eaac75c25a8b4b161bcb6/
b302a9e0a30eaac75c25a8b4b161bcb6
php
PHP
6
2016-09-30T10:27:44
True
False
False
/api/public/snipt/22849/
excerpt-length-in-wordpress
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a><span class="k">function</span> <span class="nf">new_excerpt_length</span><span class="p">(</span><span class="nv">$length</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-3"><a name="L-3"></a> <span class="k">return</span> <span class="mi">15</span><span class="p">;</span> </span><span id="L-4"><a name="L-4"></a><span class="p">}</span> </span><span id="L-5"><a name="L-5"></a><span class="nx">add_filter</span><span class="p">(</span><span class="s1">&#39;excerpt_length&#39;</span><span class="p">,</span> <span class="s1">&#39;new_excerpt_length&#39;</span><span class="p">);</span> </span><span id="L-6"><a name="L-6"></a><span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
excerpt, filter, length, wordpress

Java Server Page

JSP. JSTL. Check string length. HTML: option group tag

1
2
3
4
5
6
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"  %>
<c:if test="${fn:length(optionsVar)>0}">
	<optgroup label="OPTION_GROUP">
		<c:out value="${optionsVar}" escapeXml="false"/>
	</optgroup>
</c:if>
https://snipt.net/embed/2dc8d26e495f898fa4d02975ce66cc1f/
https://snipt.net/raw/2dc8d26e495f898fa4d02975ce66cc1f/
2dc8d26e495f898fa4d02975ce66cc1f
jsp
Java Server Page
6
2016-10-01T05:05:15
True
False
False
/api/public/snipt/4457/
jsp-jstl-get-string-length
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="k">&lt;%@</span> <span class="n">taglib</span> <span class="n">prefix</span><span class="o">=</span><span class="s">&quot;fn&quot;</span> <span class="n">uri</span><span class="o">=</span><span class="s">&quot;http://java.sun.com/jsp/jstl/functions&quot;</span> <span class="k">%&gt;</span> </span><span id="L-2"><a name="L-2"></a><span class="nt">&lt;c:if</span> <span class="na">test=</span><span class="s">&quot;${fn:length(optionsVar)&gt;0}&quot;</span><span class="nt">&gt;</span> </span><span id="L-3"><a name="L-3"></a> <span class="nt">&lt;optgroup</span> <span class="na">label=</span><span class="s">&quot;OPTION_GROUP&quot;</span><span class="nt">&gt;</span> </span><span id="L-4"><a name="L-4"></a> <span class="nt">&lt;c:out</span> <span class="na">value=</span><span class="s">&quot;${optionsVar}&quot;</span> <span class="na">escapeXml=</span><span class="s">&quot;false&quot;</span><span class="nt">/&gt;</span> </span><span id="L-5"><a name="L-5"></a> <span class="nt">&lt;/optgroup&gt;</span> </span><span id="L-6"><a name="L-6"></a><span class="nt">&lt;/c:if&gt;</span> </span></pre></div> </td></tr></table>
jsp, jstl, length, string
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.