Computer Science, asked by SomyaShirotriya, 1 year ago

store a string and print only first and last character seperately

Answers

Answered by Bilal12th
0
You can use the substring() method of java.lang.String class to remove the first or last character of String in Java. The substring() method is overloaded and provides a couple of versions which allows you to remove a character from any position in Java. Alternatively, you can convert String to StringBuffer or StringBuilder and then use its remove() method to remove the first or last character. Both StringBuffer and StringBuilder provides a convenient deleteCharAt(int index) method which removes a character from the corresponding index. You can use this method to remove both first and last character from String in Java. In the last article, I have showed you how to get the first and last character from String and today I will teach you how to remove the first and last character of String in Java. Let's see a couple of examples and how you can use these two techniques to solve this problem.


The substring() method is pretty special in Java, at least from interview point of view. Until JDK 1.7, it had a bug which keeps the reference to the original String whenever you ask for a substring. This effectively means if you have a very long String and you just need first character, reference of original String is retained even though it wasn't required. This is known as a memory leak in Java and this prompted questions like how substring() method works in Java. If you are running on JDK 1.6, it's still one of the useful detail to know about.



By the way, if you are preparing for Java programming interviews then always make sure that you have a solid understanding of various String concepts e.g. String pool, garbage collection, regular expression etc. If you are running short of time then referring book like Java Programming Interview exposed can be really beneficial. You will cover lot more topics in very short time, which are critical from interview point of view.

SomyaShirotriya: thanks but i need the program for this
Bilal12th: pls. mark as brainlist
Answered by typerisetti
0

sorry I don't know answer
Similar questions