Files
Examples/Java/Processing/processing-3.3.6/modes/java/reference/String.html
2018-04-20 10:15:15 +01:00

226 lines
10 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>String \ Language (API) \ Processing 2+</title>
<link rel="icon" href="/favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Author" content="Processing Foundation" />
<meta name="Publisher" content="Processing Foundation" />
<meta name="Keywords" content="Processing, Sketchbook, Programming, Coding, Code, Art, Design" />
<meta name="Description" content="Processing is a flexible software sketchbook and a language for learning how to code within the context of the visual arts. Since 2001, Processing has promoted software literacy within the visual arts and visual literacy within technology." />
<meta name="Copyright" content="All contents copyright the Processing Foundation, Ben Fry, Casey Reas, and the MIT Media Laboratory" />
<script src="javascript/modernizr-2.6.2.touch.js" type="text/javascript"></script>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body id="Langauge-en" onload="" >
<!-- ==================================== PAGE ============================ -->
<div id="container">
<!-- ==================================== HEADER ============================ -->
<div id="ribbon">
<ul class="left">
<li class="highlight"><a href="http://processing.org/">Processing</a></li>
<li><a href="http://p5js.org/">p5.js</a></li>
<li><a href="http://py.processing.org/">Processing.py</a></li>
<li><a href="http://android.processing.org/">Processing for Android</a></li>
</ul>
<ul class="right">
<li><a href="https://processingfoundation.org/">Processing Foundation</a></li>
</ul>
<div class="clear"></div>
</div>
<div id="header">
<a href="/" title="Back to the Processing cover."><div class="processing-logo no-cover" alt="Processing cover"></div></a>
<form name="search" method="get" action="//www.google.com/search">
<p><input type="hidden" name="as_sitesearch" value="processing.org" />
<input type="text" name="as_q" value="" size="20" class="text" />
<input type="submit" value=" " /></p>
</form>
</div>
<a id="TOP" name="TOP"></a>
<div id="navigation">
<div class="navBar" id="mainnav">
<a href="index.html" class='active'>Language</a><br>
<a href="libraries/index.html" >Libraries</a><br>
<a href="tools/index.html">Tools</a><br>
<a href="environment/index.html">Environment</a><br>
</div>
<script> document.querySelectorAll(".processing-logo")[0].className = "processing-logo"; </script>
</div>
<!-- ==================================== CONTENT - Headers ============================ -->
<div class="content">
<p class="ref-notice">This reference is for Processing 3.0+. If you have a previous version, use the reference included with your software in the Help menu. If you see any errors or have suggestions, <a href="https://github.com/processing/processing-docs/issues?state=open">please let us know</a>. If you prefer a more technical reference, visit the <a href="http://processing.github.io/processing-javadocs/core/">Processing Core Javadoc</a> and <a href="http://processing.github.io/processing-javadocs/libraries/">Libraries Javadoc</a>.</p>
<table cellpadding="0" cellspacing="0" border="0" class="ref-item">
<tr class="name-row">
<th scope="row">Name</th>
<td><h3>String</h3></td>
</tr>
<tr class=""><th scope="row">Examples</th><td><div class="example"><pre >
String str1 = "CCCP";
char data[] = {'C', 'C', 'C', 'P'};
String str2 = new String(data);
println(str1); // Prints "CCCP" to the console
println(str2); // Prints "CCCP" to the console
</pre></div>
<hr class="noshade" noshade="noshade" size="1">
<div class="example"><pre >
// Comparing String objects, see reference below.
String p = "potato";
if (p == "potato") {
println("p == potato, yep."); // This will not print
}
// The correct way to compare two Strings
if (p.equals("potato")) {
println("Yes, the contents of p and potato are the same.");
}
</pre></div>
<hr class="noshade" noshade="noshade" size="1">
<div class="example"><pre >
// Use a backslash to include quotes in a String
String quoted = "This one has &#92;"quotes&#92;"";
println(quoted); // This one has "quotes"
</pre></div>
</td></tr>
<tr class="">
<th scope="row">Description</th>
<td>
A string is a sequence of characters. The class <b>String</b> includes methods for examining individual characters, comparing strings, searching strings, extracting parts of strings, and for converting an entire string uppercase and lowercase. Strings are always defined inside double quotes (<b>"Abc"</b>), and characters are always defined inside single quotes (<b>'A'</b>).<br />
<br />
To compare the contents of two Strings, use the <b>equals()</b> method, as in <b>if (a.equals(b))</b>, instead of <b>if (a == b)</b>. A String is an Object, so comparing them with the <b>==</b> operator only compares whether both Strings are stored in the same memory location. Using the <b>equals()</b> method will ensure that the actual contents are compared. (The <a href="http://wiki.processing.org/w/Troubleshooting#Why_don.27t_these_Strings_equal.3F">troubleshooting</a> reference has a longer explanation.)<br />
<br />
Because a String is defined between double quotation marks, to include such marks within the String itself you must use the <b>&#92;</b> (backslash) character. (See the third example above.) This is known as an <em>escape sequence</em>. Other escape sequences include <b>&#92;t</b> for the tab character and <b>&#92;n</b> for new line. Because backslash is the escape character, to include a single backslash within a String, you must use two consecutive backslashes, as in: <b>&#92;&#92;</b><br />
<br />
There are more string methods than those linked from this page. Additional documentation is located online in the <a href="http://download.oracle.com/javase/6/docs/api/java/lang/String.html">official Java documentation</a>.
</td>
</tr>
<tr class=""><th scope="row">Methods</th><td><table cellpadding="0" cellspacing="0" border="0"><tr class="">
<th scope="row"><a href="String_charAt_.html">charAt()</a></th>
<td>Returns the character at the specified index</td>
</tr>
<tr class="">
<th scope="row"><a href="String_equals_.html">equals()</a></th>
<td>Compares a string to a specified object</td>
</tr>
<tr class="">
<th scope="row"><a href="String_indexOf_.html">indexOf()</a></th>
<td>Returns the index value of the first occurrence of a substring within the input string</td>
</tr>
<tr class="">
<th scope="row"><a href="String_length_.html">length()</a></th>
<td>Returns the number of characters in the input string</td>
</tr>
<tr class="">
<th scope="row"><a href="String_substring_.html">substring()</a></th>
<td>Returns a new string that is part of the input string</td>
</tr>
<tr class="">
<th scope="row"><a href="String_toLowerCase_.html">toLowerCase()</a></th>
<td>Converts all the characters to lower case</td>
</tr>
<tr class="">
<th scope="row"><a href="String_toUpperCase_.html">toUpperCase()</a></th>
<td>Converts all the characters to upper case</td>
</tr></table></td></tr>
<tr class=""><th scope="row">Constructor</th><td><pre>
String(data)
String(data, offset, length)
</pre></td></tr>
<tr class=""> <th scope="row">Parameters</th><td><table cellpadding="0" cellspacing="0" border="0"><tr class="">
<th scope="row" class="code">data</th>
<td>byte[] or char[]: either an array of bytes to be decoded into characters, or an array of characters to be combined into a string</td>
</tr>
<tr class="">
<th scope="row" class="code">offset</th>
<td>int: index of the first character</td>
</tr>
<tr class="">
<th scope="row" class="code">length</th>
<td>int: number of characters</td>
</tr></table></td> </tr>
<tr class=""><th scope="row">Related</th><td><a class="code" href="char.html">char</a><br />
<a class="code" href="text_.html">text()</a><br /></td></tr>
</table>
Updated on April 30, 2017 02:33:23pm EDT<br /><br />
<!-- Creative Commons License -->
<div class="license">
<a rel="license" href="//creativecommons.org/licenses/by-nc-sa/4.0/"><img alt="Creative Commons License" style="border: none" src="https://licensebuttons.net/l/by-nc-sa/4.0/88x31.png" /></a>
</div>
<!--
<?xpacket begin='' id=''?>
<x:xmpmeta xmlns:x='adobe:ns:meta/'>
<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>
<rdf:Description rdf:about=''
xmlns:xapRights='http://ns.adobe.com/xap/1.0/rights/'>
<xapRights:Marked>True</xapRights:Marked>
</rdf:Description>
<rdf:Description rdf:about=''
xmlns:xapRights='http://ns.adobe.com/xap/1.0/rights/'
>
<xapRights:UsageTerms>
<rdf:Alt>
<rdf:li xml:lang='x-default' >This work is licensed under a &lt;a rel=&#34;license&#34; href=&#34;http://creativecommons.org/licenses/by-nc-sa/4.0/&#34;&gt;Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License&lt;/a&gt;.</rdf:li>
<rdf:li xml:lang='en' >This work is licensed under a &lt;a rel=&#34;license&#34; href=&#34;http://creativecommons.org/licenses/by-nc-sa/4.0/&#34;&gt;Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License&lt;/a&gt;.</rdf:li>
</rdf:Alt>
</xapRights:UsageTerms>
</rdf:Description>
<rdf:Description rdf:about=''
xmlns:cc='http://creativecommons.org/ns#'>
<cc:license rdf:resource='http://creativecommons.org/licenses/by-nc-sa/4.0/'/>
</rdf:Description>
</rdf:RDF>
</x:xmpmeta>
<?xpacket end='r'?>
-->
</div>
<!-- ==================================== FOOTER ============================ -->
<div id="footer">
<div id="copyright">Processing is an open project intiated by <a href="http://benfry.com/">Ben Fry</a> and <a href="http://reas.com">Casey Reas</a>. It is developed by a <a href="about/people.html">team of volunteers</a>. </div>
<div id="colophon">
<a href="copyright.html">&copy; Info</a>
</div>
</div>
</div>
<script src="javascript/jquery-1.9.1.min.js"></script>
<script src="javascript/site.js" type="text/javascript"></script>
</body>
</html>