{"id":408,"date":"2007-10-21T00:00:00","date_gmt":"2007-10-21T00:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=408"},"modified":"2007-10-21T00:00:00","modified_gmt":"2007-10-21T00:00:00","slug":"Strings","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=408","title":{"rendered":"Strings"},"content":{"rendered":"<p><DIV id=PageTitle>Strings<\/DIV><br \/>\n<BLOCKQUOTE><!-- Strings -->Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.<br \/>\n<P>The Java platform provides the <A class=APILink href=\"http:\/\/java.sun.com\/javase\/6\/docs\/api\/java\/lang\/String.html\" target=_blank><CODE>String<\/CODE> <\/A>class to create and manipulate strings. <\/P><br \/>\n<H3 fmstyle=\"C-Head\">Creating Strings<\/H3>The most direct way to create a string is to write:<br \/>\n<BLOCKQUOTE><PRE>String greeting = &#8220;Hello world!&#8221;;<br \/>\n<\/PRE><\/BLOCKQUOTE>In this case, &#8220;Hello world!&#8221; is a <I>string literal<\/I>\u2014a series of characters in your code that is enclosed in double quotes. Whenever it encounters a string literal in your code, the compiler creates a <CODE>String<\/CODE> object with its value\u2014in this case, <CODE>Hello world!<\/CODE>.<br \/>\n<P>As with any other object, you can create <CODE>String<\/CODE> objects by using the <CODE>new<\/CODE> keyword and a constructor. The <CODE>String<\/CODE> class has 11 constructors that allow you to provide the initial value of the string using different sources, such as an array of characters: <\/P><br \/>\n<BLOCKQUOTE><PRE>char[] helloArray = { &#8216;h&#8217;, &#8216;e&#8217;, &#8216;l&#8217;, &#8216;l&#8217;, &#8216;o&#8217;, &#8216;.&#8217;};<br \/>\nString helloString = new String(helloArray);<br \/>\nSystem.out.println(helloString);<br \/>\n<\/PRE><\/BLOCKQUOTE>The last line of this code snippet displays <CODE>hello<\/CODE>.<br \/>\n<P><\/P><br \/>\n<BLOCKQUOTE><br \/>\n<HR><br \/>\n<STRONG>Note:<\/STRONG>&nbsp;The <CODE>String<\/CODE> class is immutable, so that once it is created a <CODE>String<\/CODE> object cannot be changed. The <CODE>String<\/CODE> class has a number of methods, some of which will be discussed below, that appear to modify strings. Since strings are immutable, what these methods really do is create and return a new string that contains the result of the operation.<br \/>\n<HR><br \/>\n<\/BLOCKQUOTE><br \/>\n<H3 fmstyle=\"C-Head\">String Length<\/H3>Methods used to obtain information about an object are known as <EM>accessor methods<\/EM>. One accessor method that you can use with strings is the <CODE>length()<\/CODE> method, which returns the number of characters contained in the string object. After the following two lines of code have been executed, <CODE>len<\/CODE> equals 17:<br \/>\n<BLOCKQUOTE><PRE>String palindrome = &#8220;Dot saw I was Tod&#8221;;<br \/>\nint len = palindrome.length();<br \/>\n<\/PRE><\/BLOCKQUOTE>A <I>palindrome<\/I> is a word or sentence that is symmetric\u2014it is spelled the same forward and backward, ignoring case and punctuation. Here is a short and inefficient program to reverse a palindrome string. It invokes the <CODE>String<\/CODE> method <CODE>charAt(i)<\/CODE>, which returns the i<SUP><FONT size=-1>th<\/FONT><\/SUP> character in the string, counting from 0.<br \/>\n<BLOCKQUOTE><PRE>public class StringDemo {<br \/>\n    public static void main(String[] args) {<br \/>\n        String palindrome = &#8220;Dot saw I was Tod&#8221;;<br \/>\n        int len = palindrome.length();<br \/>\n        char[] tempCharArray = new char[len];<br \/>\n        char[] charArray = new char[len];<\/p>\n<p>        \/\/ put original string in an array of chars<br \/>\n        for (int i = 0; i &lt; len; i++) {<br \/>\n            tempCharArray[i] = palindrome.charAt(i);<br \/>\n        } <\/p>\n<p>        \/\/ reverse array of chars<br \/>\n        for (int j = 0; j &lt; len; j++) {<br \/>\n            charArray[j] = tempCharArray[len &#8211; 1 &#8211; j];<br \/>\n        }<\/p>\n<p>        String reversePalindrome =  new String(charArray);<br \/>\n        System.out.println(reversePalindrome);<br \/>\n    }<br \/>\n}<\/PRE><\/BLOCKQUOTE>Running the program produces this output:<br \/>\n<BLOCKQUOTE><PRE>doT saw I was toD<br \/>\n<\/PRE><\/BLOCKQUOTE>To accomplish the string reversal, the program had to convert the string to an array of characters (first <CODE>for<\/CODE> loop), reverse the array into a second array (second <CODE>for<\/CODE> loop), and then convert back to a string. The <A class=APILink href=\"http:\/\/java.sun.com\/javase\/6\/docs\/api\/java\/lang\/String.html\" target=_blank><CODE>String<\/CODE> <\/A>class includes a method, <CODE>getChars()<\/CODE>, to convert a string, or a portion of a string, into an array of characters so we could replace the first <CODE>for<\/CODE> loop in the program above with<br \/>\n<BLOCKQUOTE><PRE>palindrome.getChars(0, len &#8211; 1, tempCharArray, 0);<br \/>\n<\/PRE><\/BLOCKQUOTE><br \/>\n<H3 fmstyle=\"C-Head\">Concatenating Strings<\/H3>The <CODE>String<\/CODE> class includes a method for concatenating two strings:<br \/>\n<BLOCKQUOTE><PRE>string1.concat(string2);<br \/>\n<\/PRE><\/BLOCKQUOTE>This returns a new string that is string1 with string2 added to it at the end.<br \/>\n<P>You can also use the <CODE>concat()<\/CODE> method with string literals, as in: <\/P><br \/>\n<BLOCKQUOTE><PRE>&#8220;My name is &#8220;.concat(&#8220;Rumplestiltskin&#8221;);<br \/>\n<\/PRE><\/BLOCKQUOTE>Strings are more commonly concatenated with the <CODE><B>+<\/B><\/CODE> operator, as in<br \/>\n<BLOCKQUOTE><PRE>&#8220;Hello,&#8221; + &#8221; world&#8221; + &#8220;!&#8221;<br \/>\n<\/PRE><\/BLOCKQUOTE>which results in<br \/>\n<BLOCKQUOTE><PRE>&#8220;Hello, world!&#8221;<br \/>\n<\/PRE><\/BLOCKQUOTE>The <CODE><B>+<\/B><\/CODE> operator is widely used in <CODE>print<\/CODE> statements. For example:<br \/>\n<BLOCKQUOTE><PRE>String string1 = &#8220;saw I was &#8220;;<br \/>\nSystem.out.println(&#8220;Dot &#8221; + string1 + &#8220;Tod&#8221;);<br \/>\n<\/PRE><\/BLOCKQUOTE>which prints<br \/>\n<BLOCKQUOTE><PRE>Dot saw I was Tod<br \/>\n<\/PRE><\/BLOCKQUOTE>Such a concatenation can be a mixture of any objects. For each object that is not a <CODE>String<\/CODE>, its <CODE>toString()<\/CODE> method is called to convert it to a <CODE>String<\/CODE>.<br \/>\n<BLOCKQUOTE><br \/>\n<HR><br \/>\n<STRONG>Note:<\/STRONG>&nbsp;The Java programming language does not permit literal strings to span lines in source files, so you must use the <CODE>+<\/CODE> concatenation operator at the end of each line in a multi-line string. For example,<br \/>\n<BLOCKQUOTE><PRE>String quote = &#8220;Now is the time for all good &#8221; +<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;men to come to the aid of their country.&#8221;;<br \/>\n<\/PRE><\/BLOCKQUOTE>Breaking strings between lines using the <CODE>+<\/CODE> concatenation operator is, once again, very common in <CODE>print<\/CODE> statements.<br \/>\n<HR><br \/>\n<\/BLOCKQUOTE><br \/>\n<H3 fmstyle=\"C-Head\">Creating Format Strings<\/H3>You have seen the use of the <CODE>printf()<\/CODE> and <CODE>format()<\/CODE> methods to print output with formatted numbers. The <CODE>String<\/CODE> class has an equivalent class method, <CODE>format()<\/CODE>, that returns a <CODE>String<\/CODE> object rather than a <CODE>PrintStream<\/CODE> object.<br \/>\n<P>Using <CODE>String&#8217;s<\/CODE> static <CODE>format()<\/CODE> method allows you to create a formatted string that you can reuse, as opposed to a one-time print statement. For example, instead of <\/P><br \/>\n<BLOCKQUOTE><PRE>System.out.printf(&#8220;The value of the float variable is %f, while the value of the &#8221; +<br \/>\n                   &#8220;integer variable is %d, and the string is %s&#8221;, floatVar, intVar, stringVar);<br \/>\n<\/PRE><\/BLOCKQUOTE>you can write<br \/>\n<BLOCKQUOTE><PRE>String fs;<br \/>\nfs = String.format(&#8220;The value of the float variable is %f, while the value of the &#8221; +<br \/>\n                   &#8220;integer variable is %d, and the string is %s&#8221;, floatVar, intVar, stringVar);<br \/>\nSystem.out.println(fs);<br \/>\n<\/PRE><\/BLOCKQUOTE><\/BLOCKQUOTE><br \/>\n<DIV><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Strings Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and manipulate strings. Creating StringsThe most direct way to create a string is to write: String greeting = &#8220;Hello world!&#8221;; In this case, &#8220;Hello &hellip; <a href=\"https:\/\/www.strongd.net\/?p=408\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Strings<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-408","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/408","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=408"}],"version-history":[{"count":0,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/408\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}