- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava_strings.java
More file actions
Latest commit
113 lines (80 loc) · 3.17 KB
/
Copy pathjava_strings.java
File metadata and controls
113 lines (80 loc) · 3.17 KB
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
//string
javaStrings
JavaStrings
Stringsareusedforstoringtext.
AStringvariablecontainsacollectionofcharacterssurroundedbydoublequotes:
ExampleGetyourownJavaServer
CreateavariableoftypeStringandassignitavalue:
Stringgreeting = "Hello";
StringLength
AStringinJavaisactuallyanobject, whichcontainmethodsthatcanperformcertainoperationsonstrings. Forexample, thelengthofastringcanbefoundwiththelength() method:
Example
Stringtxt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.println("The length of the txt string is: " + txt.length());
MoreStringMethods
Therearemanystringmethodsavailable, forexampletoUpperCase() andtoLowerCase():
Example
Stringtxt = "Hello World";
System.out.println(txt.toUpperCase()); // Outputs "HELLO WORLD"
System.out.println(txt.toLowerCase()); // Outputs "hello world"
FindingaCharacterinaString
TheindexOf() methodreturnstheindex (theposition) ofthefirstoccurrenceofaspecifiedtextinastring (includingwhitespace):
Example
Stringtxt = "Please locate where 'locate' occurs!";
System.out.println(txt.indexOf("locate")); // Outputs 7
//concatenation
JavaStringConcatenation
StringConcatenation
The + operatorcanbeusedbetweenstringstocombinethem. Thisiscalledconcatenation:
ExampleGetyourownJavaServer
StringfirstName = "John";
StringlastName = "Doe";
System.out.println(firstName + " " + lastName);
//numbers and strings
JavaNumbersandStrings
AddingNumbersandStrings
WARNING!
Javausesthe + operatorforbothadditionandconcatenation.
Numbersareadded. Stringsareconcatenated.
Ifyouaddtwonumbers, theresultwillbeanumber:
ExampleGetyourownJavaServer
intx = 10;
inty = 20;
intz = x + y; // z will be 30 (an integer/number)
Ifyouaddtwostrings, theresultwillbeastringconcatenation:
Example
Stringx = "10";
Stringy = "20";
Stringz = x + y; // z will be 1020 (a String)
Ifyouaddanumberandastring, theresultwillbeastringconcatenation:
Example
Stringx = "10";
inty = 20;
Stringz = x + y; // z will be 1020 (a String)
//special characters
avaSpecialCharacters
Strings - SpecialCharacters
Becausestringsmustbewrittenwithinquotes, Javawillmisunderstandthisstring, andgenerateanerror:
Stringtxt = "We are the so-called "Vikings" from the north.";
Thesolutiontoavoidthisproblem, istousethebackslashescapecharacter.
Thebackslash (\) escapecharacterturnsspecialcharactersintostringcharacters:
EscapecharacterResultDescription
\' 'Singlequote
\" " Double quote
\\\ Backslash
The sequence \" inserts a double quote in a string:
ExampleGet your own Java Server
String txt = "Wearetheso-called \"Vikings\" from the north.";
Thesequence \' inserts a single quote in a string:
Example
Stringtxt = "It\'s alright.";
Thesequence \\ insertsasinglebackslashinastring:
Example
Stringtxt = "The character \\ is called backslash.";
OthercommonescapesequencesthatarevalidinJavaare:
CodeResultTryit
\nNewLine
\rCarriageReturn
\tTab
\bBackspace
\fFormFeed