-
Notifications
You must be signed in to change notification settings - Fork 0
/
Char3.6.html
593 lines (263 loc) · 20.5 KB
/
Char3.6.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
<!DOCTYPE HTML>
<html lang="" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>3.6 字符串 · GitBook</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.3">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-page-toc/page-toc.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="Char3.7.html" />
<link rel="prev" href="Char3.5.html" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="Type to search" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="1.1" data-path="./">
<a href="./">
Introduction
</a>
</li>
<li class="chapter " data-level="1.2" data-path="Char1.html">
<a href="Char1.html">
第一章:Java简介
</a>
</li>
<li class="chapter " data-level="1.3" data-path="Char3.html">
<a href="Char3.html">
第三章:Java的基础编程结构
</a>
<ul class="articles">
<li class="chapter " data-level="1.3.1" data-path="Char3.1.html">
<a href="Char3.1.html">
3.1 一个简单的Java程序
</a>
</li>
<li class="chapter " data-level="1.3.2" data-path="Char3.2.html">
<a href="Char3.2.html">
3.2 注释
</a>
</li>
<li class="chapter " data-level="1.3.3" data-path="Char3.3.html">
<a href="Char3.3.html">
3.3 资料的类型
</a>
</li>
<li class="chapter " data-level="1.3.4" data-path="Char3.4.html">
<a href="Char3.4.html">
3.4 变量和常量
</a>
</li>
<li class="chapter " data-level="1.3.5" data-path="Char3.5.html">
<a href="Char3.5.html">
3.5 运算子
</a>
</li>
<li class="chapter active" data-level="1.3.6" data-path="Char3.6.html">
<a href="Char3.6.html">
3.6 字符串
</a>
</li>
<li class="chapter " data-level="1.3.7" data-path="Char3.7.html">
<a href="Char3.7.html">
3.7 输入和输出
</a>
</li>
<li class="chapter " data-level="1.3.8" data-path="Char3.8.html">
<a href="Char3.8.html">
3.8 控制流程
</a>
</li>
<li class="chapter " data-level="1.3.9" data-path="Char3.9.html">
<a href="Char3.9.html">
3.9 Big Number
</a>
</li>
<li class="chapter " data-level="1.3.10" data-path="Char3.10.html">
<a href="Char3.10.html">
3.10 Array
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4" data-path="Char4.html">
<a href="Char4.html">
第四章:对象和类
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.1" data-path="Char4.1.html">
<a href="Char4.1.html">
4.1 介绍面向对象编程
</a>
</li>
<li class="chapter " data-level="1.4.2" data-path="Char4.2.html">
<a href="Char4.2.html">
4.2 使用之前已经定义好的类
</a>
</li>
<li class="chapter " data-level="1.4.3" data-path="Char4.3.html">
<a href="Char4.3.html">
4.3 定义你自己的类
</a>
</li>
<li class="chapter " data-level="1.4.4" data-path="Char4.4.html">
<a href="Char4.4.html">
4.4 静态域和方法
</a>
</li>
<li class="chapter " data-level="1.4.5" data-path="Char4.5.html">
<a href="Char4.5.html">
4.5 方法的参数
</a>
</li>
<li class="chapter " data-level="1.4.6" data-path="Char4.6.html">
<a href="Char4.6.html">
4.6 对象的建构
</a>
</li>
<li class="chapter " data-level="1.4.7" data-path="Char4.7.html">
<a href="Char4.7.html">
4.7 包
</a>
</li>
<li class="chapter " data-level="1.4.8" data-path="Char4.8.html">
<a href="Char4.8.html">
4.8 JAR文件
</a>
</li>
<li class="chapter " data-level="1.4.9" data-path="Char4.9.html">
<a href="Char4.9.html">
4.9 文件的注释
</a>
</li>
<li class="chapter " data-level="1.4.10" data-path="Char4.10.html">
<a href="Char4.10.html">
4.10 类设计的巧思
</a>
</li>
</ul>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="." >3.6 字符串</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<h1 id="36-字串strings">3.6 字串Strings</h1>
<p>Java没有内建的string类型,而是在Java的标准库中加入了一个事先定义的类String,每一个在程序中出现的字串,都是这个String类的实例。例如:</p>
<pre><code class="lang-Java">String e =<span class="hljs-string">""</span>; <span class="hljs-comment">// an empty string</span>
String greeting = <span class="hljs-string">"Hello"</span>;
</code></pre>
<h2 id="361-substrings">3.61 Substrings:</h2>
<p>通过<code>substring</code>这个方法,你可以从一个String中抽取出一个子字串。例如:</p>
<pre><code class="lang-Java">String e = <span class="hljs-string">"Hello"</span>;
String s = s.substring(<span class="hljs-number">0</span>,<span class="hljs-number">3</span>) <span class="hljs-comment">// s = "Hel"</span>
</code></pre>
<ol>
<li>这里第一个参数表示开始的地方,第二个参数减第一个参数为字符串的长度。 </li>
</ol>
<h2 id="362-字符串的连接concatenation:">3.62 字符串的连接Concatenation:</h2>
<ol>
<li>Java可以用<code>+</code>来连接两个字符串,产生一个新的字符串。</li>
<li>当你将一个字符串和一个不是字符串的变量结合在一起的时候,整个输出的结果会变成字符串。例如:<pre><code class="lang-Java"><span class="hljs-keyword">int</span> age = <span class="hljs-number">13</span>;
String tomAge = <span class="hljs-string">"Tom"</span>+age <span class="hljs-comment">//tomAge = "Tom13"</span>
</code></pre>
在java中,你想将多个字符连接在一起,并且中间用符号区别开,你会用到join方法:<pre><code class="lang-Java">String all = String.join(<span class="hljs-string">"/"</span>,<span class="hljs-string">"S"</span>,<span class="hljs-string">"M"</span>,<span class="hljs-string">"L"</span>,<span class="hljs-string">"XL"</span>);
<span class="hljs-comment">// all is the String "S/M/L/XL"</span>
</code></pre>
</li>
</ol>
<h2 id="363-字串的不可变性(!!重要)string-are-immutable">3.63 字串的不可变性(!!重要)String Are Immutable</h2>
<p>对于已经生成的字串,String类没有提供方法来让你改变字符串内容,如果你要改变某个字符串变量a的值,你可以有几种不同的方法:</p>
<ol>
<li>通过concatenation和substring两个来进行删减和增加达到change的目的。</li>
<li>直接将a指向别的字符串,等于直接更改a指向的地址。</li>
</ol>
<h2 id="365-空字串和null">3.65 空字串和Null</h2>
<p>空字串<code>""</code>的长度为0,空字串是一个有着字符串长度(0),并且没有内容的Java对象。String对象还可以有一个特殊的值,叫做<code>null</code>,这表示当前没有任何一个对象指向现在的变数。在Java中,在你测试是不是空字串的时候,你需要以下的程式码:</p>
<pre><code class="lang-Java"><span class="hljs-keyword">if</span> (str != <span class="hljs-keyword">null</span> && str.length() != <span class="hljs-number">0</span>)
</code></pre>
<ol>
<li>要注意,这个顺序不可以换,必须先测试这个string是不是null的,因为null.lenghth()会报错。</li>
</ol>
<h2 id="369-stringbuilder">3.69 StringBuilder</h2>
<p>除了concatenation这个方法,Java还有append方法来合并两个字串。
在将多个string结合在一起的时候,如果使用concatenation这个方法,每一次相加就会产生一个新的String,非常的麻烦。为此,Java用stringbuilder来解决这个问题,利用<code>builder.appened()</code>这个方法,这样不会每次拼接都产生新的string,而是会保存在一个缓存区里面。</p>
<pre><code class="lang-Java">String str = builder.toString();
</code></pre>
<ol>
<li>当你确定已经结束拼接之后,可以将其变回string:</li>
</ol>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="Char3.5.html" class="navigation navigation-prev " aria-label="Previous page: 3.5 运算子">
<i class="fa fa-angle-left"></i>
</a>
<a href="Char3.7.html" class="navigation navigation-next " aria-label="Next page: 3.7 输入和输出">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"3.6 字符串","level":"1.3.6","depth":2,"next":{"title":"3.7 输入和输出","level":"1.3.7","depth":2,"path":"Char3.7.md","ref":"Char3.7.md","articles":[]},"previous":{"title":"3.5 运算子","level":"1.3.5","depth":2,"path":"Char3.5.md","ref":"Char3.5.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["page-toc","-lunr","livereload"],"pluginsConfig":{"page-toc":{"position":"before-first","selector":".markdown-section h1, .markdown-section h2, .markdown-section h3, .markdown-section h4","showByDefault":true},"lunr":{"maxIndexSize":200000},"livereload":{},"highlight":{},"search":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"Char3.6.md","mtime":"2019-05-20T07:33:36.114Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-05-21T11:49:25.827Z"},"basePath":".","book":{"language":""}});
});
</script>
</div>
<script src="gitbook/gitbook.js"></script>
<script src="gitbook/theme.js"></script>
<script src="gitbook/gitbook-plugin-page-toc/anchor-3.1.1.min.js"></script>
<script src="gitbook/gitbook-plugin-page-toc/page-toc.js"></script>
<script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
<script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
<script src="gitbook/gitbook-plugin-search/search.js"></script>
<script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
<script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
</body>
</html>