Programming Articles - Page 2299 of 3366

Program to match vowels in a string using regular expression in Java

Maruthi Krishna
Updated on 21-Nov-2019 07:32:02

5K+ Views

You can group all the required characters to match within the square braces “[ ]” i.e. The metacharacter/sub-expression “[ ]” matches all the specified characters. Therefore, to match all the letters specify the vowel letters within these as shown below −[aeiouAEIOU]Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatchVowels {    public static void main( String args[] ) {       String regex = "[aeiouAEIOU]";       System.out.println("Enter input string: ");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       //Compiling the regular expression       Pattern.compile(regex); ... Read More

Moving all Upper case letters to the end of the String using Java RegEx

Maruthi Krishna
Updated on 21-Nov-2019 07:29:15

776 Views

The subexpression “[ ]” matches all the characters specified in the braces. Therefore, to move all the upper case letters to the end of a string −Iterate through all the characters in the given string.Match all the upper case letters in the given string using the regular expression "[A-Z]".Concatenate the special characters and the remaining characters to two different strings.Finally, concatenate the special characters string to the other string.Example 1public class RemovingSpecialCharacters {    public static void main(String args[]) {       String input = "sample B text C with G upper case LM characters in between";     ... Read More

Moving all special char to the end of the String using Java Regular Expression RegEx)

Maruthi Krishna
Updated on 21-Nov-2019 07:26:50

2K+ Views

The following regular expression matches all the special characters i.e. all characters except English alphabet spaces and digits."[^a-zA-Z0-9\s+]"To move all the special characters to the end of the given line, match all the special characters using this regex concatenate them to an empty string and concatenate remaining characters to another string finally, concatenate these two strings.Example 1public class RemovingSpecialCharacters {    public static void main(String args[]) {       String input = "sample # text * with & special@ characters";       String regex = "[^a-zA-Z0-9\s+]";       String specialChars = "";       String inputData ... Read More

How to match a particular word in a string using Pattern class in Java?

Maruthi Krishna
Updated on 21-Nov-2019 07:24:54

1K+ Views

The \b meta character in Java regular expressions matches the word boundaries Therefore to find a particular word from the given input text specify the required word within the word boundaries in the regular expressions as −"\brequired word\b";Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MachingWordExample1 {    public static void main( String args[] ) {       //Reading string value       Scanner sc = new Scanner(System.in);       System.out.println("Enter input string");       String input = sc.next();       //Regular expression to find digits       String regex = "\bhello\b";   ... Read More

Matcher usePattern() method in Java with Examples

Maruthi Krishna
Updated on 21-Nov-2019 07:22:43

123 Views

The java.util.regex.Matcher class represents an engine that performs various match operations. There is no constructor for this class, you can create/obtain an object of this class using the matches() method of the class java.util.regex.Pattern.The usePattern() method of the Matcher class accepts a Pattern object representing a new regex pattern and uses it to find matches.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class UsePatternExample {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input text: ");       String input = sc.nextLine();       String regex = "[#%&*]"; ... Read More

C++ program for Find sum of odd factors of a number

Sunidhi Bansal
Updated on 09-Jul-2020 08:22:32

575 Views

Given with a positive integer and the task is to generate the odd factors of a number and finding out the sum of given odd factors.ExampleInput-: number = 20 Output-: sum of odd factors is: 6 Input-: number = 18 Output-: sum of odd factors is: 13So, result = 1 + 5 = 6Approach used in the below program is as follows −Input the number for calculating the sum of odd factors of that numberIgnore the digit 0 and 2 because both are even digits and store the digit 1 because it is an odd digitStart the loop from 3 ... Read More

C++ Program for triangular pattern (mirror image around 0)

Sunidhi Bansal
Updated on 09-Jul-2020 08:23:06

347 Views

Given with the positive value n and the task is to generate the triangular pattern i.e. mirror image of the printed numbers and display the resultExampleInput-: n = 6 Output-:Input-: n = 3 Output-:Approach used in the below program is as follows −Input the value of n as a positive integerTraverse one loop i for the number of rows in a pattern i.e. nTraverse one loop j for the number of spaces in a patternTraverse another loop for the digits in a patternAlgorithmSTART Step 1-> declare function to print mirror image of triangular pattern    void print_mirror(int n)    declare ... Read More

C++ Program for converting hours into minutes and seconds

Sunidhi Bansal
Updated on 09-Jul-2020 08:23:37

1K+ Views

Given with the input as hours and the task is to convert the number of hours into minutes and seconds and display the corresponding resultFormula used for converting the hours into minutes and seconds is −1 hour = 60 minutes    Minutes = hours * 60 1 hour = 3600 seconds    Seconds = hours * 3600ExampleInput-: hours = 3 Output-: 3 hours in minutes are 180    3 hours in seconds are 10800 Input-: hours = 5 Output-: 5 hours in minutes are 300    5 hours in seconds are 18000Approach used in the below program is as follows ... Read More

Java program to convert Byte array to IP Address

Sunidhi Bansal
Updated on 24-Sep-2024 21:44:00

1K+ Views

In this article, we will learn to convert a byte array into an IP address using the IPAddress class in Java. This program takes a series of bytes, representing an IP address in binary form, and converts them into a standard dot-separated string representation of an IP address. We'll go through the simple steps of declaring a byte array and then converting it into a readable IP address format. What is a Byte Array? A byte comprises 8 bits and a byte array comprises contiguous bytes which store the binary information. In Java, a byte is a primitive datatype that can ... Read More

What to use @SerializedName annotation using Gson in Java?

raja
Updated on 09-Jul-2020 08:17:53

8K+ Views

The @SerializedName annotation can be used to serialize a field with a different name instead of an actual field name. We can provide the expected serialized name as an annotation attribute, Gson can make sure to read or write a field with the provided name.Syntax@Retention(value=RUNTIME) @Target(value={FIELD, METHOD}) public @interface SerializedNameExampleimport com.google.gson.*; import com.google.gson.annotations.*; public class SerializedNameTest {    public static void main(String args[]) {       Gson gson = new GsonBuilder().setPrettyPrinting().create();       Person person = new Person(115, "Raja Ramesh", "Hyderabad");       String jsonStr = gson.toJson(person);       System.out.println(jsonStr);    } } // Person class class ... Read More

Advertisements