Found 2616 Articles for Java

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

1K+ 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

949 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

60 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

Java program to convert Byte array to IP Address

Sunidhi Bansal
Updated on 20-Nov-2019 13:10:10

742 Views

Given with a Byte array ad the task is to convert it into an IP address using the IPAddress class in java and display the result.What is a Byte ArrayA byte comprises of 8 bits and byte array comprises of contiguous bytes which stores the binary information. In java, byte is a primitive datatype that can be understood as the byte of computer i.e. it is of 8 bits and it can hold values ranging for -128 to 127.Declaring a byte − byte name_of_byte_variable = initializer;Declaring a byte array − byte[] name_of_byte_array = new byte[];What is an IPAddress ClassIn java, ... Read More

What to use @SerializedName annotation using Gson in Java?

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

7K+ 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

How to implement custom JsonAdapter using Gson in Java?

raja
Updated on 09-Jul-2020 08:15:08

2K+ Views

The @JsonAdapter annotation can be used at field or class level to specify the Gson. The TypeAdapter class can be used to convert Java objects to and from JSON. By default, Gson library converts application classes to JSON by using built-in type adapters but we can override it by providing custom type adapters.Syntax@Retention(value=RUNTIME) @Target(value={TYPE, FIELD}) public @interface JsonAdapterExampleimport java.io.IOException; import com.google.gson.Gson; import com.google.gson.TypeAdapter; import com.google.gson.annotations.JsonAdapter; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; public class JsonAdapterTest {    public static void main(String[] args) {       Gson gson = new Gson();       System.out.println(gson.toJson(new Customer()));    } } // Customer class class Customer { ... Read More

Matcher quoteReplacement(String s) method in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 07:43:12

287 Views

The appendReplacement() method of the Matcher class accepts a StringBuffer object and a String (replacement string) as parameters and, appends the input data to the StringBuffer object, replacing the matched content with the replacement string.Internally, this method reads each character from the input string and appends the String buffer, whenever a match occurs it appends the replacement string instead of matched content part of the string to the buffer and, proceeds from the next position of the matched substring.While passing the replacement string to this method if you use “/” or “$” they will not be considered as regular characters ... Read More

Matcher hitEnd() method in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 07:38:51

65 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 hitEnd() method verifies whether the end of the input data reached during the previous match if so, it returns true else false. If this method returns true it indicates that more input data might change the result of the match.For example, if you are trying to match the last word of an input string to you using the regex “you$” and if your 1st input line ... Read More

Matcher useAnchoringBounds() method in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 07:32:57

79 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 anchoring bounds are used to match the region matches such as ^ and $. By default, a matcher uses anchoring bounds.The useAnchoringBounds() method of this class method accepts a boolean value and, if you pass true to this method the current matcher uses anchoring bounds and if you pass false to this method it uses non-anchoring bounds.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Trail ... Read More

Matcher hasTransparentBounds() method in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 07:29:16

46 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.In regular expressions, the lookbehind and lookahead constructs are used to match a particular pattern that is preceding or, succeeding in some other pattern. For example, if you need to accept a string which accepts 5 to 12 characters the regular expression will be −"\A(?=\w{6, 10}\z)";By default, the boundaries of the matcher region are not transparent to the constructs lookahead, lookbehind and, boundary matching i.e. These constructs ... Read More

Advertisements