Skip to content

Commit 441da8a

Browse files
author
rfadatare
committed
string api guide
0 parents  commit 441da8a

File tree

158 files changed

+1366
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

158 files changed

+1366
-0
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.javaguides.stringhandling</groupId>
4+
<artifactId>java-strings-guide</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
</project>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.javaguides.stringbuffer.methods;
2+
3+
public class AppendExample {
4+
public static void main(String[] args) {
5+
// 14 append overloaded methods
6+
7+
// Append String
8+
StringBuffer buffer;
9+
buffer = new StringBuffer().append("guides");
10+
System.out.println("Append String : " + buffer);
11+
12+
// Append char
13+
buffer = new StringBuffer().append('c');
14+
System.out.println("Append char : " + buffer);
15+
16+
// Append Object
17+
buffer = new StringBuffer().append(new Object().getClass());
18+
System.out.println("Append Object : " + buffer);
19+
20+
// Append chars
21+
char[] chars = { 'j', 'a', 'v', 'a' };
22+
buffer = new StringBuffer().append(chars);
23+
System.out.println("Append chars : " + buffer);
24+
25+
// Append charSequence
26+
CharSequence charSequence = new String("charSequence");
27+
buffer = new StringBuffer().append(charSequence);
28+
System.out.println("Append charSequence : " + buffer);
29+
30+
// Append Double
31+
buffer = new StringBuffer().append(10.0d);
32+
System.out.println("Append Double : " + buffer);
33+
34+
// Append Float
35+
buffer = new StringBuffer().append(10.5f);
36+
System.out.println("Append Float : " + buffer);
37+
38+
// Append int
39+
buffer = new StringBuffer().append(100);
40+
System.out.println("Append int : " + buffer);
41+
42+
// Append Boolean
43+
buffer = new StringBuffer().append(true);
44+
System.out.println("Append Boolean : " + buffer);
45+
46+
// Append Long
47+
buffer = new StringBuffer().append(1000);
48+
System.out.println("Append Long : " + buffer);
49+
50+
// Append stringbuffer
51+
buffer = new StringBuffer().append(new StringBuffer("stringbuffer"));
52+
System.out.println("Append stringbuffer : " + buffer);
53+
54+
// Appends the string representation of a subarray of the char array
55+
// argument to this sequence.
56+
buffer = new StringBuffer().append(chars, 1, 3);
57+
System.out.println("Appends the string representation of a "
58+
+ " subarray of the char array argument to this sequence. : " + buffer);
59+
60+
// Appends a subsequence of the specified CharSequence to this sequence
61+
buffer = new StringBuffer().append("javaguides", 0, 9);
62+
System.out.println("Appends a subsequence of the specified "
63+
+ " CharSequence to this sequence. : " + buffer);
64+
65+
// Appends the string representation of the codePoint argument to this
66+
// sequence.
67+
buffer = new StringBuffer().appendCodePoint(5);
68+
System.out.println(
69+
"Appends the string representation of the "
70+
+ " codePoint argument to this sequence. : " + buffer);
71+
}
72+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.javaguides.stringbuffer.methods;
2+
3+
public class CapacityExample {
4+
public static void main(String[] args) {
5+
StringBuffer builder = new StringBuffer("javaguides");
6+
int capacity = builder.capacity();
7+
8+
// inital capacity
9+
System.out.println(new StringBuffer().capacity());
10+
11+
// intial capacity 16 + number of characters in string
12+
System.out.println("Capacity of the string :: " + capacity);
13+
}
14+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.javaguides.stringbuffer.methods;
2+
3+
public class ChatAtExample {
4+
public static void main(String[] args) {
5+
charAtExample1();
6+
//charAtExample2();
7+
charAtExample3();
8+
}
9+
10+
public static void charAtExample1() {
11+
StringBuffer buffer = new StringBuffer("Welcome to string handling guide");
12+
char ch1 = buffer.charAt(0);
13+
char ch2 = buffer.charAt(5);
14+
char ch3 = buffer.charAt(11);
15+
char ch4 = buffer.charAt(20);
16+
System.out.println("Character at 0 index is: " + ch1);
17+
System.out.println("Character at 5th index is: " + ch2);
18+
System.out.println("Character at 11th index is: " + ch3);
19+
System.out.println("Character at 20th index is: " + ch4);
20+
}
21+
22+
// IndexOutOfBoundsException - if the index argument is negative or not less
23+
// than the length of this string.
24+
public static void charAtExample2() {
25+
StringBuffer builder = new StringBuffer("Java Guides");
26+
char ch1 = builder.charAt(builder.length());
27+
System.out.println("character :: " + ch1);
28+
}
29+
30+
// How to get first and last chanracter of the string
31+
public static void charAtExample3() {
32+
StringBuffer buffer = new StringBuffer("Java Guides");
33+
int strLength = buffer.length() - 1;
34+
// Fetching first character
35+
System.out.println("Character at 0 index is: " + buffer.charAt(0));
36+
// The last Character is present at the string length-1 index
37+
System.out.println("Character at last index is: " + buffer.charAt(strLength));
38+
}
39+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.javaguides.stringbuffer.methods;
2+
3+
public class CodePointAtExample {
4+
public static void main(String[] args) {
5+
StringBuffer buffer = new StringBuffer("javaguides");
6+
int unicode = buffer.codePointAt(0);
7+
System.out.println("the character (Unicode code point) at the specified index is :: " + unicode);
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.javaguides.stringbuffer.methods;
2+
3+
public class CodePointBeforeExample {
4+
public static void main(String[] args) {
5+
StringBuffer buffer = new StringBuffer("javaguides");
6+
int unicode = buffer.codePointBefore(1);
7+
System.out.println("the character (Unicode code point) at the before specified index is :: " + unicode);
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.javaguides.stringbuffer.methods;
2+
3+
public class CodePointCountExample {
4+
public static void main(String[] args) {
5+
StringBuffer buffer = new StringBuffer("javaguides");
6+
System.out.println("length of the string :: " + buffer.length());
7+
int unicode = buffer.codePointCount(0, buffer.length());
8+
System.out.println("the character (Unicode code point) at the specified index is :: " + unicode);
9+
}
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.javaguides.stringbuffer.methods;
2+
3+
public class DeleteExample {
4+
public static void main(String[] args) {
5+
StringBuffer buffer = new StringBuffer("javaguides");
6+
7+
// start with index and end with end -1
8+
StringBuffer subBuffer = buffer.delete(0, 4);
9+
System.out.println("Delete string 'java' from string 'javaguides' : " + subBuffer.toString());
10+
11+
StringBuffer subBuilder1 = new StringBuffer("javaguides").deleteCharAt(4);
12+
System.out.println("Delete char 'g' from string 'javaguides' : " + subBuilder1);
13+
}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.javaguides.stringbuffer.methods;
2+
3+
public class EnsureCapacityExample {
4+
public static void main(String[] args) {
5+
StringBuffer builder = new StringBuffer();
6+
builder.ensureCapacity(11);
7+
System.out.println(builder.capacity());
8+
builder.ensureCapacity(17);
9+
System.out.println(builder.capacity());
10+
}
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.javaguides.stringbuffer.methods;
2+
3+
public class GetCharsExample {
4+
public static void main(String[] args) {
5+
StringBuffer buffer = new StringBuffer("javaguides");
6+
char[] dst = new char[buffer.length()];
7+
buffer.getChars(0, buffer.length(), dst, 0);
8+
9+
for (char c : dst) {
10+
System.out.println(c);
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)