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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.javaguides.stringbuffer.methods;
2+
3+
public class IndexOfExample {
4+
public static void main(String[] args) {
5+
StringBuffer buffer = new StringBuffer("javaguides");
6+
7+
// method 1
8+
int index = buffer.indexOf("guides");
9+
System.out.println(index);
10+
11+
// method2
12+
index = buffer.indexOf("guides", 3);
13+
System.out.println(index);
14+
}
15+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.javaguides.stringbuffer.methods;
2+
3+
public class InsertExample {
4+
public static void main(String[] args) {
5+
6+
// 12 insert overloaded method
7+
StringBuffer builder = new StringBuffer("javaguides").insert(1,true);
8+
System.out.println(builder.toString());
9+
10+
builder = new StringBuffer("javaguides").insert(0, 'J');
11+
System.out.println(builder.toString());
12+
13+
char[] chars = {'d','e','v','e','l','o','p','e','r'};
14+
builder = new StringBuffer("javaguides").insert(4, chars);
15+
System.out.println(builder.toString());
16+
17+
CharSequence charSequence = new StringBuilder("J2EE/");
18+
builder = new StringBuffer("javaguides").insert(0, charSequence);
19+
System.out.println(builder.toString());
20+
21+
builder = new StringBuffer("javaguides").insert(0, 100.0d);
22+
System.out.println(builder.toString());
23+
24+
builder = new StringBuffer("javaguides").insert(0, 100.0f);
25+
System.out.println(builder.toString());
26+
27+
builder = new StringBuffer("javaguides").insert(0, 100);
28+
System.out.println(builder.toString());
29+
30+
builder = new StringBuffer("javaguides").insert(0, 100l);
31+
System.out.println(builder.toString());
32+
33+
builder = new StringBuffer("javaguides").insert(0, new Object());
34+
System.out.println(builder.toString());
35+
36+
builder = new StringBuffer("javaguides").insert(0, "ultimate");
37+
System.out.println(builder.toString());
38+
39+
builder = new StringBuffer("javaguides").insert(0, chars, 0, chars.length);
40+
System.out.println(builder.toString());
41+
42+
builder = new StringBuffer("javaguides").insert(0, charSequence, 0, charSequence.length());
43+
System.out.println(builder.toString());
44+
45+
}
46+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.javaguides.stringbuffer.methods;
2+
3+
public class LastIndexOfExample {
4+
public static void main(String[] args) {
5+
StringBuffer buffer = new StringBuffer("javaguides");
6+
7+
// method1
8+
int lastIndexOf = buffer.lastIndexOf("guides");
9+
System.out.println(" last index of given string 'guides' in' "
10+
+ buffer.toString()+"' :: " + lastIndexOf);
11+
12+
// method 2
13+
lastIndexOf = buffer.lastIndexOf("java", 3);
14+
System.out.println(" last index of given string 'java' in' "
15+
+ buffer.toString()+"' :: " + lastIndexOf);
16+
}
17+
}
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 LengthExample {
4+
public static void main(String[] args) {
5+
StringBuffer buffer = new StringBuffer("javaguides");
6+
int length = buffer.length();
7+
System.out.println(" length of the string '" + buffer + "' is :: " + length);
8+
}
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.javaguides.stringbuffer.methods;
2+
3+
public class ReplaceExample {
4+
public static void main(String[] args) {
5+
StringBuffer buffer = new StringBuffer("jaguides");
6+
7+
// replace ja with java- start index 0 and end index -1
8+
StringBuffer subBuffer = buffer.replace(0, 2, "java");
9+
10+
System.out.println(subBuffer);
11+
}
12+
}
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 ReverseExample {
4+
public static void main(String[] args) {
5+
StringBuffer buffer = new StringBuffer("javaguides");
6+
StringBuffer reverse = buffer.reverse();
7+
System.out.println("Reversed string :" + reverse);
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 SetCharExample {
4+
public static void main(String[] args) {
5+
StringBuffer buffer = new StringBuffer("javaguides");
6+
buffer.setCharAt(0, 'J');
7+
System.out.println(buffer.toString());
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 SetLengthExample {
4+
public static void main(String[] args) {
5+
StringBuffer buffer = new StringBuffer("javaguides");
6+
System.out.println("Before set length to 0 : " + buffer.length());
7+
buffer.setLength(0);
8+
System.out.println("After set length to 0 : " + buffer.length());
9+
}
10+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.javaguides.stringbuffer.methods;
2+
3+
public class StringBufferConstructorExamples {
4+
public static void main(String[] args) {
5+
6+
// Constructor 1
7+
// Constructs a string buffer with no characters in it and an initial
8+
// capacity of 16 characters.
9+
StringBuffer buffer = new StringBuffer();
10+
// System.out.println(buffer.capacity());
11+
// Constructor 2
12+
// Constructs a string buffer initialized to the contents of the
13+
// specified string. The initial capacity of the string buffer is 16
14+
// plus the length of the string argument.
15+
16+
StringBuffer buffer2 = new StringBuffer("javaguides");
17+
// System.out.println(buffer2.capacity());
18+
19+
// Constructor 3
20+
// Constructs a string buffer that contains the same characters as the
21+
// specified CharSequence. The initial capacity of the string buffer is
22+
// 16 plus the length of the CharSequence argument.
23+
CharSequence charSequence = new StringBuilder("charSequence");
24+
StringBuffer buffer3 = new StringBuffer(charSequence);
25+
System.out.println(buffer3);
26+
// Constructor 4
27+
// Constructs a string builder with no characters in it and an initial
28+
// capacity specified by the capacity argument.
29+
StringBuffer buffer4 = new StringBuffer(20);
30+
//System.out.println(buffer4.capacity());
31+
}
32+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.javaguides.stringbuffer.methods;
2+
3+
public class SubStringExample {
4+
public static void main(String[] args) {
5+
StringBuffer buffer = new StringBuffer("javaguides");
6+
7+
// substring from start to end
8+
String subStr = buffer.substring(0, buffer.length());
9+
System.out.println("substring from 0 to length of the string : " + subStr);
10+
11+
// print java
12+
System.out.println(buffer.substring(0, 4));
13+
14+
// print guides
15+
System.out.println(buffer.substring(4, buffer.length()));
16+
}
17+
}

0 commit comments

Comments
 (0)