snipt

Ctrl+h for KB shortcuts

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/
/raw/2dc8d26e495f898fa4d02975ce66cc1f/
2dc8d26e495f898fa4d02975ce66cc1f
jsp
Java Server Page
6
2019-06-20T11:55:59
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></span><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

Java

Add leading 0

1
2
3
4
/*
 * Add 0 to stretch i to 2 digits.
 */
String.format("%02d", i);
https://snipt.net/embed/a636bb43a275ad4e4ffba5c9d2077e31/
/raw/a636bb43a275ad4e4ffba5c9d2077e31/
a636bb43a275ad4e4ffba5c9d2077e31
java
Java
4
2019-05-30T07:11:11
True
False
False
/api/public/snipt/3654/
add-leading-zeroes
<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></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cm">/*</span> </span><span id="L-2"><a name="L-2"></a><span class="cm"> * Add 0 to stretch i to 2 digits.</span> </span><span id="L-3"><a name="L-3"></a><span class="cm"> */</span> </span><span id="L-4"><a name="L-4"></a><span class="n">String</span><span class="o">.</span><span class="na">format</span><span class="o">(</span><span class="s">&quot;%02d&quot;</span><span class="o">,</span> <span class="n">i</span><span class="o">);</span> </span></pre></div> </td></tr></table>
java, numbers, string

Java

Convert string to enum equivalent

1
2
3
4
5
6
7
8
enum Colors {
  BLUE,
  WHITE
}

// ..

Colors blue = Enum.valueOf(Colors.class, "BLUE");
https://snipt.net/embed/874a923a13e2a05915e4202150f2eb46/
/raw/874a923a13e2a05915e4202150f2eb46/
874a923a13e2a05915e4202150f2eb46
java
Java
8
2019-06-22T07:40:50
True
False
False
/api/public/snipt/3648/
convert-string-to-enum-equivalent
<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></span><span id="L-1"><a name="L-1"></a><span class="kd">enum</span> <span class="n">Colors</span> <span class="o">{</span> </span><span id="L-2"><a name="L-2"></a> <span class="n">BLUE</span><span class="o">,</span> </span><span id="L-3"><a name="L-3"></a> <span class="n">WHITE</span> </span><span id="L-4"><a name="L-4"></a><span class="o">}</span> </span><span id="L-5"><a name="L-5"></a> </span><span id="L-6"><a name="L-6"></a><span class="c1">// ..</span> </span><span id="L-7"><a name="L-7"></a> </span><span id="L-8"><a name="L-8"></a><span class="n">Colors</span> <span class="n">blue</span> <span class="o">=</span> <span class="n">Enum</span><span class="o">.</span><span class="na">valueOf</span><span class="o">(</span><span class="n">Colors</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> <span class="s">&quot;BLUE&quot;</span><span class="o">);</span> </span></pre></div> </td></tr></table>
conversion, enum, string

Ruby irb session

string to class

"my_class".camelize.constantize
https://snipt.net/embed/f0d916787d3a8454f23daa7b2918fb19/
/raw/f0d916787d3a8454f23daa7b2918fb19/
f0d916787d3a8454f23daa7b2918fb19
rbcon
Ruby irb session
1
2019-06-20T00:39:21
True
False
False
/api/public/snipt/3502/
string-to-class
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="go">&quot;my_class&quot;.camelize.constantize</span> </span></pre></div> </td></tr></table>
constant, rails, string

JavaScript

remove all spaces from a string

/* remove all spaces from a string */
var1.split(' ').join('');
https://snipt.net/embed/16d331a9b990fc4ce98db4e4b99051b4/
/raw/16d331a9b990fc4ce98db4e4b99051b4/
16d331a9b990fc4ce98db4e4b99051b4
js
JavaScript
2
2019-06-21T20:22:27
True
False
False
/api/public/snipt/3272/
remove-all-spaces-from-a-string
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cm">/* remove all spaces from a string */</span> </span><span id="L-2"><a name="L-2"></a><span class="nx">var1</span><span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">).</span><span class="nx">join</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">);</span> </span></pre></div> </td></tr></table>
"remove all spaces", js, string