Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
Core Java and Java 8
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Lokesh Singh
Core Java and Java 8
Commits
e2131ce5
Unverified
Commit
e2131ce5
authored
May 08, 2022
by
Lokesh Singh
Committed by
GitHub
May 08, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create stringMethods.java
parent
961571b9
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
95 additions
and
0 deletions
+95
-0
stringMethods.java
String/stringMethods.java
+95
-0
No files found.
String/stringMethods.java
0 → 100644
View file @
e2131ce5
// Java code to illustrate different constructors and methods
// String class.
import
java.io.*
;
import
java.util.*
;
class
Test
{
public
static
void
main
(
String
[]
args
)
{
String
s
=
"GeeksforGeeks"
;
// or String s= new String ("GeeksforGeeks");
// Returns the number of characters in the String.
System
.
out
.
println
(
"String length = "
+
s
.
length
());
// Returns the character at ith index.
System
.
out
.
println
(
"Character at 3rd position = "
+
s
.
charAt
(
3
));
// Return the substring from the ith index character
// to end of string
System
.
out
.
println
(
"Substring "
+
s
.
substring
(
3
));
// Returns the substring from i to j-1 index.
System
.
out
.
println
(
"Substring = "
+
s
.
substring
(
2
,
5
));
// Concatenates string2 to the end of string1.
String
s1
=
"Geeks"
;
String
s2
=
"forGeeks"
;
System
.
out
.
println
(
"Concatenated string = "
+
s1
.
concat
(
s2
));
// Returns the index within the string
// of the first occurrence of the specified string.
String
s4
=
"Learn Share Learn"
;
System
.
out
.
println
(
"Index of Share "
+
s4
.
indexOf
(
"Share"
));
// Returns the index within the string of the
// first occurrence of the specified string,
// starting at the specified index.
System
.
out
.
println
(
"Index of a = "
+
s4
.
indexOf
(
'a'
,
3
));
// Checking equality of Strings
Boolean
out
=
"Geeks"
.
equals
(
"geeks"
);
System
.
out
.
println
(
"Checking Equality "
+
out
);
out
=
"Geeks"
.
equals
(
"Geeks"
);
System
.
out
.
println
(
"Checking Equality "
+
out
);
out
=
"Geeks"
.
equalsIgnoreCase
(
"gEeks "
);
System
.
out
.
println
(
"Checking Equality "
+
out
);
//If ASCII difference is zero then the two strings are similar
int
out1
=
s1
.
compareTo
(
s2
);
System
.
out
.
println
(
"the difference between ASCII value is="
+
out1
);
// Converting cases
String
word1
=
"GeeKyMe"
;
System
.
out
.
println
(
"Changing to lower Case "
+
word1
.
toLowerCase
());
// Converting cases
String
word2
=
"GeekyME"
;
System
.
out
.
println
(
"Changing to UPPER Case "
+
word2
.
toUpperCase
());
// Trimming the word
String
word4
=
" Learn Share Learn "
;
System
.
out
.
println
(
"Trim the word "
+
word4
.
trim
());
// Replacing characters
String
str1
=
"feeksforfeeks"
;
System
.
out
.
println
(
"Original String "
+
str1
);
String
str2
=
"feeksforfeeks"
.
replace
(
'f'
,
'g'
)
;
System
.
out
.
println
(
"Replaced f with g -> "
+
str2
);
}
}
// output:
// String length = 13
// Character at 3rd position = k
// Substring ksforGeeks
// Substring = eks
// Concatenated string = GeeksforGeeks
// Index of Share 6
// Index of a = 8
// Checking Equality false
// Checking Equality true
// Checking Equality false
// the difference between ASCII value is=-31
// Changing to lower Case geekyme
// Changing to UPPER Case GEEKYME
// Trim the word Learn Share Learn
// Original String feeksforfeeks
// Replaced f with g -> geeksgorgeeks
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment