Last active
August 29, 2015 14:20
-
-
Save rurtubia/c253249f70ef0b244a7f to your computer and use it in GitHub Desktop.
StringBuilder Practice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Given: | |
| StringBuilder b1 = new StringBuilder("snorkler"); | |
| StringBuilder b2 = new StringBuilder("yoodler"); | |
| b1.append(b2.substring(2,5).toUpperCase()); | |
| //b1 = snorkler b2 = yoodler | |
| b1.append("odl".toUpperCase()); | |
| //b1 = snorkler b2 = yoodler | |
| b1.append("ODL"); | |
| //b1 = snorklerODL b2 = yoodler | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Given: | |
| StringBuilder b1 = new StringBuilder("snorkler"); | |
| StringBuilder b2 = new StringBuilder("yoodler"); | |
| b2.insert(3, b1.append("a")); | |
| //b1 = snorklera //b2 = yoodler | |
| b2, insert(3, "snorklera"); | |
| //b1 = snorklera //b2 = yoosnorkleradler |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Given: | |
| StringBuilder b1 = new StringBuilder("snorkler"); | |
| StringBuilder b2 = new StringBuilder("yoodler"); | |
| b1.replace(3, 4, b2.substring(4).append(b2.append(false))); | |
| //b1 = "snorkler" b2 = "yoodlerfalse" | |
| b1.replace(3, 4, b2.substring(4).append("yoodlerfalse")); | |
| //b1 = "snorkler" b2 = "lerfalseyoodlerfalse" | |
| b1.replace(3, 4, "lerfalseyoodlerfalse"); | |
| //b1 = "snolerfalseyoodlerfalsekler" b2 = "lerfalseyoodlerfalse" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Java.lang.StringBuilder.insert() Method | |
| // Description | |
| // The java.lang.StringBuilder.insert(int offset, String str) method inserts the | |
| // string into this character sequence. | |
| // The characters of the String argument are inserted, in order, into this sequence | |
| // at the indicated offset, moving up any characters originally above that position | |
| // and increasing the length of this sequence by the length of the argument. | |
| //Declaration | |
| public StringBuilder insert(int offset, String str) | |
| //Parameters | |
| // offset -- This is the offset. | |
| // str -- This is the value of string. | |
| //Return Value | |
| //Exception | |
| StringIndexOutOfBoundsException -- if the offset is invalid. | |
| //Example | |
| package com.tutorialspoint; | |
| import java.lang.*; | |
| public class StringBuilderDemo { | |
| public static void main(String[] args) { | |
| StringBuilder str = new StringBuilder("tutorials website"); | |
| System.out.println("string = " + str); | |
| // insert string value at offset 9 | |
| str.insert(9, "point"); | |
| // prints StringBuilder after insertion | |
| System.out.print("After insertion = "); | |
| System.out.println(str.toString()); | |
| } | |
| } | |
| //Let us compile and run the above program, this will produce the following result: | |
| string = tutorials website | |
| After insertion = tutorialspoint website |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Java.lang.StringBuilder.substring() Method | |
| //Description: | |
| // The java.lang.StringBuilder.substring(int start, int end) method returns a new | |
| // String that contains a subsequence of characters currently contained in this sequence. | |
| // The substring begins at the specified start and extends to the character at index end - 1. | |
| //Declaration | |
| public String substring(int start, int end) | |
| //Parameters | |
| //start -- This is the beginning index, inclusive. | |
| //end -- This is the ending index, exclusive. | |
| //Return Value | |
| //This method returns the new string. | |
| //Exception | |
| StringIndexOutOfBoundsException | |
| //-- if start or end are negative or greater than length(), or start is greater than end. | |
| //Example | |
| package com.tutorialspoint; | |
| import java.lang.*; | |
| public class StringBuilderDemo { | |
| public static void main(String[] args) { | |
| StringBuilder str = new StringBuilder("admin"); | |
| System.out.println("string = " + str); | |
| // prints substring from index 3 | |
| System.out.println("substring is = " + str.substring(3)); | |
| /* prints substring from index 1 to 4 excluding character | |
| at 4th index */ | |
| System.out.println("substring is = " + str.substring(1, 4)); | |
| } | |
| } | |
| Let us compile and run the above program, this will produce the following result: | |
| string = admin | |
| substring is = in | |
| substring is = dmi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment