Skip to content

Commit 718f15c

Browse files
committed
Java String Methods
1 parent 99f8b07 commit 718f15c

File tree

1 file changed

+46
-46
lines changed

1 file changed

+46
-46
lines changed

java-string-methods.md

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,188 +1,188 @@
11
# Java String Methods
22

3-
1. **char charAt(int index)**
3+
## char charAt(int index)
44

55
It returns the character at the specified index. Specified index value should be between 0 to length() -1 both inclusive. It throws IndexOutOfBoundsException if index<0||>= length of String.
66

7-
1. **boolean equals(Object obj)**
7+
## boolean equals(Object obj)
88

99
Compares the string with the specified string and returns true if both matches else false.
1010

11-
1. **boolean equalsIgnoreCase(String string)**
11+
## boolean equalsIgnoreCase(String string)
1212

1313
It works same as equals method but it doesn\'t consider the case while comparing strings. It does a case insensitive comparison.
1414

15-
1. **int compareTo(String string)**
15+
## int compareTo(String string)
1616

1717
This method compares the two strings based on the Unicode value of each character in the strings.
1818

19-
1. **int compareToIgnoreCase(String string)**
19+
## int compareToIgnoreCase(String string)
2020

2121
Same as CompareTo method however it ignores the case during comparison.
2222

23-
1. **boolean startsWith(String prefix, int offset)**
23+
## boolean startsWith(String prefix, int offset)
2424

2525
It checks whether the substring (starting from the specified offset index) is having the specified prefix or not.
2626

27-
1. **boolean startsWith(String prefix)**
27+
## boolean startsWith(String prefix)
2828

2929
It tests whether the string is having specified prefix, if yes then it returns true else false.
3030

31-
1. **boolean endsWith(String suffix)**
31+
## boolean endsWith(String suffix)
3232

3333
Checks whether the string ends with the specified suffix.
3434

35-
1. **int hashCode()**
35+
## int hashCode()
3636

3737
It returns the hash code of the string.
3838

39-
1. **int indexOf(int ch)**
39+
## int indexOf(int ch)
4040

4141
Returns the index of first occurrence of the specified character ch in the string.
4242

43-
1. **int indexOf(int ch, int fromIndex)**
43+
## int indexOf(int ch, int fromIndex)
4444

4545
Same as indexOf method however it starts searching in the string from the specified fromIndex.
4646

47-
1. **int lastIndexOf(int ch)**
47+
## int lastIndexOf(int ch)
4848

4949
It returns the last occurrence of the character ch in the string.
5050

51-
1. **int lastIndexOf(int ch, int fromIndex)**
51+
## int lastIndexOf(int ch, int fromIndex)
5252

5353
Same as lastIndexOf(int ch) method, it starts search from fromIndex.
5454

55-
1. **int indexOf(String str)**
55+
## int indexOf(String str)
5656

5757
This method returns the index of first occurrence of specified substring str.
5858

59-
1. **int lastindexOf(String str)**
59+
## int lastindexOf(String str)
6060

6161
Returns the index of last occurrence of string str.
6262

63-
1. **String substring(int beginIndex)**
63+
## String substring(int beginIndex)
6464

6565
It returns the substring of the string. The substring starts with the
6666
character at the specified index.
6767

68-
1. **String substring(int beginIndex, int endIndex)**
68+
## String substring(int beginIndex, int endIndex)
6969

7070
Returns the substring. The substring starts with character at beginIndex and ends with the character at endIndex.
7171

72-
1. **String concat(String str)**
72+
## String concat(String str)
7373

7474
Concatenates the specified string “str” at the end of the string.
7575

76-
1. **String replace(char oldChar, char newChar)**
76+
## String replace(char oldChar, char newChar)
7777

7878
It returns the new updated string after changing all the occurrences of oldChar with the newChar.
7979

80-
1. **boolean contains(CharSequence s)**
80+
## boolean contains(CharSequence s)
8181

8282
It checks whether the string contains the specified sequence of char values. If yes then it returns true else false. It throws NullPointerException of ‘s’ is null.
8383

84-
1. **String toUpperCase(Locale locale)**
84+
## String toUpperCase(Locale locale)
8585

8686
Converts the string to upper case string using the rules defined by specified locale.
8787

88-
1. **String toUpperCase()**
88+
## String toUpperCase()
8989

9090
Equivalent to toUpperCase(Locale.getDefault()).
9191

92-
1. **public String intern()**
92+
## public String intern()
9393

9494
This method searches the specified string in the memory pool and if it is found then it returns the reference of it, else it allocates the memory space to the specified string and assign the reference to it.
9595

96-
1. **public boolean isEmpty()**
96+
## public boolean isEmpty()
9797

9898
This method returns true if the given string has 0 length. If the length of the specified Java String is non-zero then it returns false.
9999

100-
1. **public static String join()**
100+
## public static String join()
101101

102102
This method joins the given strings using the specified delimiter and returns the concatenated Java String
103103

104-
1. **String replaceFirst(String regex, String replacement)**
104+
## String replaceFirst(String regex, String replacement)
105105

106106
It replaces the first occurrence of substring that fits the given regular expression “regex” with the specified replacement string.
107107

108-
1. **String replaceAll(String regex, String replacement)**
108+
## String replaceAll(String regex, String replacement)
109109

110110
It replaces all the occurrences of substrings that fits the regular expression regex with the replacement string.
111111

112-
1. **String[] split(String regex, int limit)**
112+
## String[] split(String regex, int limit)
113113

114114
It splits the string and returns the array of substrings that matches the given regular expression. limit is a result threshold here.
115115

116-
1. **String[] split(String regex)**
116+
## String[] split(String regex)
117117

118118
Same as split(String regex, int limit) method however it does not have any threshold limit.
119119

120-
1. **String toLowerCase(Locale locale)**
120+
## String toLowerCase(Locale locale)
121121

122122
It converts the string to lower case string using the rules defined by given locale.
123123

124-
1. **public static String format()**
124+
## public static String format()
125125

126126
This method returns a formatted java String
127127

128-
1. **String toLowerCase()**
128+
## String toLowerCase()
129129

130130
Equivalent to toLowerCase(Locale. getDefault()).
131131

132-
1. **String trim()**
132+
## String trim()
133133

134134
Returns the substring after omitting leading and trailing white spaces from the original
135135
string.
136136

137-
1. **char[] toCharArray()**
137+
## char[] toCharArray()
138138

139139
Converts the string to a character array.
140140

141-
1. **static String copyValueOf(char[] data)**
141+
## static String copyValueOf(char[] data)
142142

143143
It returns a string that contains the characters of the specified character array.
144144

145-
1. **static String copyValueOf(char[] data, int offset, int count)**
145+
## static String copyValueOf(char[] data, int offset, int count)
146146

147147
Same as above method with two extra arguments – initial offset of subarray and length of subarray.
148148

149-
1. **void getChars(int srcBegin, int srcEnd, char[] dest, int destBegin)**
149+
## void getChars(int srcBegin, int srcEnd, char[] dest, int destBegin)
150150

151151
It copies the characters of src array to the dest array. Only the specified range is being copied(srcBegin to srcEnd) to the dest subarray(starting fromdestBegin).
152152

153-
1. **static String valueOf()**
153+
## static String valueOf()
154154

155155
This method returns a string representation of passed arguments such as int, long, float, double, char and char array.
156156

157-
1. **boolean contentEquals(StringBuffer sb)**
157+
## boolean contentEquals(StringBuffer sb)
158158

159159
It compares the string to the specified string buffer.
160160

161-
1. **boolean regionMatches(int srcoffset, String dest, int destoffset, int len)**
161+
## boolean regionMatches(int srcoffset, String dest, int destoffset, int len)
162162

163163
It compares the substring of input to the substring of specified string.
164164

165-
1. **boolean regionMatches(boolean ignoreCase, int srcoffset, String dest, int destoffset, int len)**
165+
## boolean regionMatches(boolean ignoreCase, int srcoffset, String dest, int destoffset, int len)
166166

167167
Another variation of regionMatches method with the extra boolean argument to specify whether the comparison is case sensitive or case insensitive.
168168

169-
1. **byte[] getBytes(String charsetName)**
169+
## byte[] getBytes(String charsetName)
170170

171171
It converts the String into sequence of bytes using the specified charset encoding and returns the array of resulted bytes.
172172

173-
1. **byte[] getBytes()**
173+
## byte[] getBytes()
174174

175175
This method is similar to the above method it just uses the default charset encoding for converting the string into sequence of bytes.
176176

177-
1. **int length()**
177+
## int length()
178178

179179
It returns the length of a String.
180180

181-
1. **boolean matches(String regex)**
181+
## boolean matches(String regex)
182182

183183
It checks whether the String is matching with the specified regular expression regex.
184184

185-
1. **int codePointAt(int index)**
185+
## int codePointAt(int index)
186186

187187
It is similar to the charAt method however it returns the Unicode code point value of specified index rather than the character itself.
188188

0 commit comments

Comments
 (0)