Found 34494 Articles for Programming

Python object persistence (shelve)

George John
Updated on 25-Jun-2020 13:48:57

4K+ Views

The shelve module in Python’s standard library is a simple yet effective tool for persistent data storage when using a relational database solution is not required. The shelf object defined in this module is dictionary-like object which is persistently stored in a disk file. This creates afile similar to dbm database on UNIX like systems. Only string data type can be used as key in this special dictionary object, whereas any picklable object can serve as value.The shelve module defines three classes as follows −Sr.No.Module & Description1ShelfThis is the base class for shelf implementations. It is initialized with dict-like object.2BsdDbShelf This ... Read More

Java Program to subtract week from current date

Samual Sam
Updated on 25-Jun-2020 13:50:00

482 Views

Firstly, you need to import the following package for Calendar class in Java.import java.util.Calendar;Create a Calendar object and display the current date and time.Calendar calendar = Calendar.getInstance(); System.out.println("Current Date and Time = " + calendar.getTime());Now, let us decrement the weeks using the calendar.add() method and Calendar.WEEK_OF_YEAR constant. Set a negative value since we are decrementing here.calendar.add(Calendar.WEEK_OF_YEAR, -2);Example Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Subtract 2 weeks       calendar.add(Calendar.WEEK_OF_YEAR, -2);     ... Read More

Java program to print all distinct elements of a given integer array in Java

karthikeya Boyini
Updated on 25-Jun-2020 13:51:31

5K+ Views

All distinct elements of an array are printed i.e. all the elements in the array are printed only once and duplicate elements are not printed. An example of this is given as follows.Array = 1 5 9 1 4 9 6 5 9 7 Distinct elements of above array = 1 5 9 4 6 7A program that demonstrates this is given as follows.Example Live Demopublic class Example {    public static void main (String[] args) {       int arr[] = {1, 5, 9, 1, 4, 9, 6, 5, 9, 7};       int n = arr.length;   ... Read More

Java program to program to cyclically rotate an array by one

Samual Sam
Updated on 25-Jun-2020 13:18:13

883 Views

The array is cyclically rotated clockwise by one. This means that each of the array elements are displayed to the right by one and the last element ends up as the first element. An example of this is given as follows.Original array = 1 2 3 4 5 6 7 8 9 10 Rotated array = 10 1 2 3 4 5 6 7 8 9A program that demonstrates this is given as follows.Example Live Demopublic class Example {    public static void main(String[] args) {       int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, ... Read More

Java Program to replace only first occurrences of given String with new one

karthikeya Boyini
Updated on 25-Jun-2020 13:19:35

204 Views

Use the replaceFirst() method to replace only first occurrences of given String with new one.Let’s say we have the following string.String str = "THIS IS DEMO TEXT!";We have to replace the first occurrence of “IS” with “EV”. For that, use replaceFirst() method.str.replaceFirst("IS", "EV");The following is the final example, wherein the first occurrence of “IS” is replaced.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "THIS IS DEMO TEXT!";       System.out.println("String = "+str);       System.out.println("Replacing only the first occurrence of substring IS...");       System.out.println("Updated string = ... Read More

Java Program to replace all occurrences of a given character with new character

Samual Sam
Updated on 25-Jun-2020 13:21:05

290 Views

Let’s say the following is our string.THIS IS DEMO TEXT!Here, to replace every occurrence of ‘I’ with ‘E’, use the replace() method.str.replace('I', 'E'));The following is the complete example to replace all occurrences of a given character with new character.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "THIS IS DEMO TEXT!";       System.out.println("String = "+str);       System.out.println("Updated string = "+str.replace('I', 'E'));    } }OutputString = THIS IS DEMO TEXT! Updated string = THES ES DEMO TEXT!

Replacing Substrings in a Java String

karthikeya Boyini
Updated on 25-Jun-2020 13:23:11

113 Views

Let’s say the following is our string.String str = "The Walking Dead!";We want to replace the substring “Dead” with “Alive”. For that, let us use the following logic. Here, we have used a while loop and within that found the index of the substring to be replaced. In this way, one by one we have replaced the entire substring.int beginning = 0, index = 0; StringBuffer strBuffer = new StringBuffer(); while ((index = str.indexOf(subStr1, beginning)) >= 0) {    strBuffer.append(str.substring(beginning, index));    strBuffer.append(subStr2);    beginning = index + subStr1.length(); }The following is the complete example to replace substrings.Example Live Demopublic class ... Read More

Java Program to remove whitespace from the beginning and end of a string

karthikeya Boyini
Updated on 25-Jun-2020 13:24:39

315 Views

Use the trim() method to remove whitespace from the beginning and end of a string.Let’s say the following is our string with whitespace.str1 = " The Big Bang Theory ";Now, let us trim all the whitespaces from the beginning and the end.String str2 = str1.trim();The following is the final example with output.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str1 = " The Big Bang Theory ";       System.out.println("String: "+str1);       String str2 = str1.trim();       System.out.println("Updated string (trimming spaces): "+str2);    } }OutputString: The Big ... Read More

Java Program to replace one specific character with another

Samual Sam
Updated on 25-Jun-2020 13:26:35

509 Views

Use the replace() method to replace a specific character with another. Let’s say the following is our string and here we are replacing a whitespace with a $ character.String str1 = "Orange is the new Black!";Now, use the replace() method to replace a character with $str1.replace(' ', '$');Example Live Demopublic class Demo {    public static void main(String[] args) {       String str1 = "Orange is the new Black!";       System.out.println("String: "+str1);       String str2 = str1.replace(' ', '$');       System.out.println("Updated string: "+str2);    } }OutputString: Orange is the new Black! Updated string: ... Read More

Java Program to access character of a string

karthikeya Boyini
Updated on 14-Jun-2024 14:44:43

31K+ Views

To access character of a string in Java, use the charAt() method. The position is to be added as the parameter.Let’s say we have the following string −String str = "laptop";Let’s find the character at the 4th position using charAt() method.str.charAt(3));The following is the final example.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "laptop";       System.out.println("String: "+str);       // finding character at 4th position       System.out.println("Character at 4th position: "+str.charAt(3));    } }OutputString: laptop Character at 4th position: t

Advertisements