{"id":226,"date":"2003-01-20T23:47:47","date_gmt":"2003-01-21T03:47:47","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=226"},"modified":"2003-01-20T23:47:47","modified_gmt":"2003-01-21T03:47:47","slug":"manipulate-expressions-with-the-new-features-of-javalangstring","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2003\/01\/20\/manipulate-expressions-with-the-new-features-of-javalangstring\/","title":{"rendered":"Manipulate expressions with the new features of java.lang.String"},"content":{"rendered":"<p>From builder.com: &#8220;Manipulate expressions with the new features of java.lang.String&#8221;<\/p>\n<p>&#8220;The Java String class has remained largely unchanged since JDK 1.0,<br \/>\nreceiving only a minor addition of new methods with JDK 1.2. However, there<br \/>\nhave been some major additions in <a href=\"http:\/\/java.sun.com\/j2se\/1.4\/docs\/api\/overview-summary.html\">JDK 1.4<\/a>.<\/p>\n<p>Regular expressions have arrived with much fanfare in JDK 1.4, but the<br \/>\nmelding of the <a href=\"http:\/\/java.sun.com\/j2se\/1.4\/docs\/api\/java\/lang\/String.html\">java.lang.String<\/a> class to the <a href=\"http:\/\/java.sun.com\/j2se\/1.4\/docs\/api\/java\/util\/regex\/package-summary.html\">java.util.regexp<\/a> package has<br \/>\nbeen less talked about. Four new regexp-based methods have arrived, with<br \/>\ntheir various overloads, to help enhance the String class.<\/p>\n<p>The <a href=\"http:\/\/java.sun.com\/j2se\/1.4\/docs\/api\/java\/lang\/String.html#matches(java.lang.String)\">String.matches(String)<\/a> method returns true if the current String<br \/>\nmatches a given regular expression. For example:<\/p>\n<p>&#8220;Music&#8221;.matches(&#8220;M.*&#8221;) returns true while<br \/>\n&#8220;Noise&#8221;.matches(&#8220;M.*&#8221;) returns false.<\/p>\n<p>The <a href=\"http:\/\/java.sun.com\/j2se\/1.4\/docs\/api\/java\/lang\/String.html#replaceAll(java.lang.String, java.lang.String)\">String.replaceFirst(String, String)<\/a> method replaces the first<br \/>\ninstance of a regular expression with a replacement value and returns the<br \/>\nnew<br \/>\nversion of the String. For example:<\/p>\n<p>&#8220;Small angry, angry kittens&#8221;.replaceFirst(&#8220;angry&#8221;, &#8220;fluffy&#8221;)<\/p>\n<p>will give us:<\/p>\n<p>&#8220;Small fluffy, angry kittens&#8221;.<\/p>\n<p>We also have the replaceAll method, which is exactly the same except<br \/>\nthat it will replace all the values. So we get:<\/p>\n<p>&#8220;Small fluffy, fluffy kittens&#8221;.<\/p>\n<p>The first argument to a replace method is a regular expression, while<br \/>\nthe second argument is the replacement value. This replacement value may<br \/>\ncontain references to captured values.<\/p>\n<p>Lastly, we have the String.split(String) method, which turns a String<br \/>\ninto an array of Strings, based on a common delimiter. The following code<br \/>\nshows how to split a line of comma-separated values:<\/p>\n<p>    String csv = &#8220;one,two, three,four,  five&#8221;;<br \/>\n    String[] fields = csv.split(&#8220;,\\s*&#8221;);<\/p>\n<p>The argument may be a regular expression, which allows the code in this<br \/>\ninstance to ignore the white space characters.<\/p>\n<p>The addition of regular expressions to Java is a long-awaited affair,<br \/>\nbut the new helper methods in java.lang.String are an added bonus that<br \/>\nshould reduce the regular expression learning curve.&#8221;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>From builder.com: &#8220;Manipulate expressions with the new features of java.lang.String&#8221; &#8220;The Java String class has remained largely unchanged since JDK 1.0, receiving only a minor addition of new methods with JDK 1.2. However, there have been some major additions in JDK 1.4. Regular expressions have arrived with much fanfare in JDK 1.4, but the melding &hellip; <a href=\"https:\/\/cephas.net\/blog\/2003\/01\/20\/manipulate-expressions-with-the-new-features-of-javalangstring\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Manipulate expressions with the new features of java.lang.String<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/226"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=226"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/226\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}