Maruthi Krishna has Published 951 Articles

How to replace all occurrences of a word in a string with another word in java?

Maruthi Krishna

Maruthi Krishna

Updated on 10-Oct-2019 10:40:12

935 Views

The replaceAll() method of the String class accepts two strings, One representing a regular expression to find a string and the other representing a replacement string.And, replaces all the matched sequences with the given String. Therefore, to replace a particular word with another in a String −Get the required String.Invoke ... Read More

How to delete a string inside a file(.txt) in java?

Maruthi Krishna

Maruthi Krishna

Updated on 10-Oct-2019 09:16:10

6K+ Views

The replaceAll() method accepts a regular expression and a String as parameters and, matches the contents of the current string with the given regular expression, in case of match, replaces the matched elements with the String.To delete a particular String from a file using the replaceAll() method −Retrieve the contents ... Read More

How to insert a string in beginning of another string in java?

Maruthi Krishna

Maruthi Krishna

Updated on 10-Oct-2019 09:08:29

5K+ Views

Using a character arrayGet the both strings, suppose we have a string str1 and the string to be added at begin of str1 is str2.Create a character array with the sum of lengths of the two Strings as its length.Starting from 0th position fill each element in the array with ... Read More

What is ARM in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 10-Oct-2019 08:59:24

775 Views

A resource is an object which implements AutoClosable interface. whenever you use a resource in your program it is recommended to close it after the usage.Initially, this task is done using the finally block.Exampleimport java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Scanner; public class FinalExample {    public static void main(String[] ... Read More

What is the role of the String intern() method in java?

Maruthi Krishna

Maruthi Krishna

Updated on 10-Oct-2019 08:55:14

94 Views

A String is a class in Java which stores a sequence of characters, it belongs to the java.lang package. Once you create a String object you cannot modify them (immutable).StorageAll String objects are stored in a separate memory location in the heap area known as, String Constant pool.Whenever you define ... Read More

What are the rules of exception handling with respect to method overriding in java?

Maruthi Krishna

Maruthi Krishna

Updated on 10-Oct-2019 08:27:41

626 Views

While a superclass method throws an exception while overriding it you need to follow the certain rules.Should throw Same exception or, sub typeIf the super-class method throws certain exception, the method in the sub-class should throw the same exception or its sub type.ExampleIn the following example, the readFile() method of ... Read More

How do we split a string with any whitespace chars as delimiters using java?

Maruthi Krishna

Maruthi Krishna

Updated on 10-Oct-2019 08:26:17

631 Views

The split() method of the String class accepts a delimiter (in the form of the string), divides the current String into smaller strings based on the delimiter and returns the resulting strings as an array. If the String does not contain the specified delimiter this method returns an array which ... Read More

How do we mix two strings and generate another in java?

Maruthi Krishna

Maruthi Krishna

Updated on 10-Oct-2019 08:20:46

14K+ Views

Strings are used to store a sequence of characters in Java, they are treated as objects. The String class of the java.lang package represents a String.You can create a String either by using the new keyword (like any other object) or, by assigning value to the literal (like any other ... Read More

How do we split a string on a fixed character sequence in java?

Maruthi Krishna

Maruthi Krishna

Updated on 10-Oct-2019 08:19:19

482 Views

The split() method of the String class accepts a delimiter (in the form of the string), divides the current String into smaller strings based on the delimiter and returns the resulting strings as an array. If the String does not contain the specified delimiter this method returns an array which ... Read More

How do we create a string from the contents of a file in java?

Maruthi Krishna

Maruthi Krishna

Updated on 10-Oct-2019 07:24:03

138 Views

In Java you can read the contents of a file in several ways one way is to read it to a string using the java.util.Scanner class, to do so, Instantiate the Scanner class, with the path of the file to be read, as a parameter to its constructor.Create an empty ... Read More

Advertisements