<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Character &#8211; CodePills.com</title>
	<atom:link href="https://codepills.com/tag/character/feed/" rel="self" type="application/rss+xml" />
	<link>https://codepills.com</link>
	<description>Helping you make a better code</description>
	<lastBuildDate>Thu, 26 May 2022 13:53:35 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>How to figure out if a character in a string is a number</title>
		<link>https://codepills.com/how-to-figure-out-if-a-character-in-a-string-is-a-number/</link>
					<comments>https://codepills.com/how-to-figure-out-if-a-character-in-a-string-is-a-number/#respond</comments>
		
		<dc:creator><![CDATA[Andrej Buday]]></dc:creator>
		<pubDate>Tue, 03 May 2022 13:49:33 +0000</pubDate>
				<category><![CDATA[Tips & tricks]]></category>
		<category><![CDATA[char]]></category>
		<category><![CDATA[Character]]></category>
		<category><![CDATA[exceptions]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[regular expression]]></category>
		<category><![CDATA[String]]></category>
		<guid isPermaLink="false">https://codepills.com/?p=1301</guid>

					<description><![CDATA[This article is about multiple ways to pull a character from a string and check if it is a number. <a href="https://codepills.com/how-to-figure-out-if-a-character-in-a-string-is-a-number/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[<p>This article will show how to figure out if a character in a string is a number. You might be wondering how to do it. There are multiple ways to determine if an individual or series of characters in the string classify as a number. So let&#8217;s look at various approaches for determining if the character is a number.</p>
<p><span id="more-1301"></span></p>
<h2>Using <em>Character</em> class</h2>
<p>First option is to select a character from string with help of <em>charAt()</em> method and the character position; and use <em>Character</em> class static method <em>isDigit()</em> to return <em>boolean</em> value.</p>
<pre><code class="language-java">String movie = "101 dalmatians";
System.out.println(movie);
boolean result = Character.isDigit(movie.charAt(0));
System.out.println("Is first letter number? Answer: " + result)</code></pre>
<p>The output will be:</p>
<pre><code class="language-bash">101 dalmatians
Is first letter number? true</code></pre>
<p>If we want to check if several characters are numbers, we can place the <em>isDigit()</em> method call inside the loop.</p>
<pre><code class="language-java">String movie = "101 dalmatians";
for (int i = 0; i <= movie.length() - 1; i++) {
    System.out.println(movie.charAt(i) + " -> " + Character.isDigit(movie.charAt(i)));
}</code></pre>
<p>The output of the above code will be:</p>
<pre><code class="language-bash">1 -> true
0 -> true
1 -> true
  -> false
d -> false
a -> false
l -> false
m -> false
a -> false
t -> false
i -> false
a -> false
n -> false
s -> false</code></pre>
<h2>Using Unicode check</h2>
<p>Using <em>Character</em> class static method <em>isDigit()</em> can be used on any Unicode character, not just 0-9 number range. You can use create number check at any <b>Unicode</b> character.</p>
<p>Here is an example of checking a number through a check of Unicode characters:</p>
<pre><code class="language-java">String movie = "101 dalmatians";
System.out.println(movie);
boolean char c = word.charAt(0);
boolean isDigit = (c >= '0' && c <= '9');
System.out.println(isDigit)</code></pre>
<pre><code class="language-bash">101 dalmatians
true</code></pre>
<h2>Using Regex</h2>
<p>Alternative way to using <em>Character</em> class static method <em>isDigit()</em> is to use solution based on <i>regex</i> output. However, this will require string to be instance of <em>String</em> class as we can conduct regex pattern search only on <em>String</em>.</p>
<p>Let us show you first regex pattern for searching digit in substring of length 1 on the instance <em>String</em> class named <em>word</em>:</p>
<pre><code class="language-java">word.substring(0, 1).matches("\\d")</code></pre>
<p>The alternative and equivalent version uses the regex search pattern with the search of digit range.</p>
<pre><code class="language-java">word.substring(0, 1).matches("[0-9]")</code></pre>
<p>The regex solution should be discouraged as it is an over-engineered solution. While there might be a minor performance hit, we can argue it would not affect simple coding solutions. But be aware that regex brings complexity. A solution implementing regular expression should be used with caution and with a fair amount of tests.</p>
<p><b>Note:</b> Regular expressions are very strong, but expensive tool. It is valid to use regular expressions for character check if, e.g. the first character is a digit. But performance-wise, it is also like shooting birds with cannon.</p>
<h2>Using casting</h2>
<p>The last way to check if the character in a string is a number is to use Java casting feature. We will cast string values into one of the numerical object classes. However, this is the worst way how to do it. If you find a code using casting, you should refactor it immediately.</p>
<pre><code class="language-java">public boolean isNumber(String word) {
    boolean isNumber = true;
    try {
        Integer.valueOf(word.substring(0,1));
    } catch (Exception e) {
        isNumber = false;
    }
    return isNumber;
}</code></pre>
<p>There is a lot that can go wrong with the method above. If the entering argument word is an empty string, casting will throw <em>NumberFormatException</em> and catch it. If the first character is not a digit, e.g. letter or special symbol, casting will also throw <em>NumberFormatException</em> and catch it. But if the first character is a digit, it will return true.</p>
<p>You can place any numerical object class to the code such as <em>Integer</em>, <em>Double</em>, <em>Float</em> or <em>Long</em>. But please do not! Using casting with <em>valueOf()</em> method is wrong for two reason:</p>
<ul>
<li>In general, we want to avoid throwing <em>Exceptions</em>, checked or unchecked, as they slow the program and have tremendous performance hit.</li>
<li>Second reason, we can see above at least 3 different ways which provide shorter, more effective and readable way for solving the same problem.</li>
</ul>
<h2>Conclusion</h2>
<p>In the article, we have demonstrated four different ways to find out if the single character or characters from a string is a number or not. The solutions ranged from best to worst, from easiest to most cumbersome. Use as much as possible the first option which use <em>Character</em> class static method <em>isDigit()</em> for determining if the character is a string. Using <em>isDigit()</em> method is, without instantiation of any variables, the most effective and readable solution.</p>
<p>I hope you learned something new and had fun. Thanks for the reading. And if you have any tricks or know a better way <u>how to find out if the character is a string</u>, let us know in the comments below. We would like to hear your ideas.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codepills.com/how-to-figure-out-if-a-character-in-a-string-is-a-number/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
