Searching Characters and Substring in a String in Java

AmitDiwan
Updated on 14-Sep-2020 08:44:41

655 Views

Following is the Java program to search characters and substring in a string −Example Live Demoimport java.io.*; import java.lang.*; public class Demo {    public static void main (String[] args) {       String test_str = "Hello";       CharSequence seq = "He";       boolean bool_1 = test_str.contains(seq);       System.out.println("Was the substring found? " + bool_1);       boolean bool_2 = test_str.contains("Lo");       System.out.println("Was the substring found? " + bool_2);    } }OutputWas the substring found? true Was the substring found? FalseA class named Demo contains the main function. Here, a string ... Read More

JavaScript Parent and Child Classes with Same Method Name

AmitDiwan
Updated on 14-Sep-2020 08:42:46

1K+ Views

Yes, parent and child classes can have a method with the same name.Exampleclass Parent {    constructor(parentValue) {       this.parentValue = parentValue;    }    //Parent class method name which is same as Child Class method name.    showValues() {       console.log("The parent method is called.....");       console.log("the value is="+this.parentValue);    } } class Child extends Parent {    constructor(parentValue, childValue){       super(parentValue);       this.childValue = childValue;    }    //Child class method name which is same as Parent Class method name.    showValues() {       console.log("The child ... Read More

Filter JSON Data with Multiple Objects

AmitDiwan
Updated on 14-Sep-2020 08:40:38

17K+ Views

To filter JSON data with multiple objects, you can use the concept of filter along with ==.Exampleconst jsonObject= [    {       studentId:101,       studentName:"David"    },    {       studentId:102,       studentName:"Mike"    },    {       studentId:103,       studentName:"David"    },    {       studentId:104,       studentName:"Bob"    } ] var result=jsonObject.filter(obj=> obj.studentName == "David"); console.log(result);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo194.js. This will produce the following output −PS C:\Users\Amit\javascript-code> node demo194.js [    { studentId: 101, studentName: 'David' },    { studentId: 103, studentName: 'David' } ]

Display Asian and American Date Time with Date Object in JavaScript

AmitDiwan
Updated on 14-Sep-2020 08:39:14

2K+ Views

For this, you can use timeZone from JavaScript i.e. specific time zones for Asia and America respectively.For Asian Time Zonevar todayDateTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Kolkata"});For American Time Zonevar americaDateTime = new Date().toLocaleString("en-US", {timeZone: "America/New_York"});Examplevar todayDateTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Kolkata"}); todayDateTime = new Date(todayDateTime); console.log("The Asia Date time is="); console.log(todayDateTime) var americaDateTime = new Date().toLocaleString("en-US", {timeZone: "America/New_York"}); americaDateTime = new Date(americaDateTime); console.log("The America Date time is="); console.log(americaDateTime);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo193.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo193.js The Asia Date time is= ... Read More

Display Selected Value of Dropdown in JavaScript Console

AmitDiwan
Updated on 14-Sep-2020 08:37:45

3K+ Views

Let’s say the following is our dropdown (select) −    Javascript    MySQL    MongoDB    Java Following is the code to display the selected value on Console −Example Live Demo Document Javascript MySQL MongoDB Java    function selectedSubjectName() {       var subjectIdNode = document.getElementById('subjectName');       var value =       subjectIdNode.options[subjectIdNode.selectedIndex].text;       console.log("The selected value=" + value);    } To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option ... Read More

Local Variable Type Inference (LVTI) in Java 10

AmitDiwan
Updated on 14-Sep-2020 08:36:17

446 Views

Type inference in Java refers to the automatic detection of the variable’s datatype. This automatic detection usually happens at compile time. It is a feature of Java 10 and it allows the developer to skip declaring the type that is associated with the local variables. Local variables are those which are defined inside a method, initialization block, in for-loops, etc. The type would be usually identified by JDK.Upto Java 9, the following syntax was used to define local variable of a class type −class_name variable_name = new class_name(Arguments);This way, the type of the object would be specified on the right ... Read More

Invoke Parent's Method in JavaScript When Child Has Same Method Name

AmitDiwan
Updated on 14-Sep-2020 08:31:46

1K+ Views

In order to call the parent method when both parent and child have the same method name and signature.You can use the below syntax −console.log(yourParentClassName.prototype.yourMethodName.call(yourChildObjectName));Exampleclass Super {    constructor(value) {       this.value = value;    }    display() {       return `The Parent class value is= ${this.value}`;    } } class Child extends Super {    constructor(value1, value2) {       super(value1);       this.value2 = value2;    }    display() {       return `${super.display()}, The Child Class value2       is=${this.value2}`;    } } var childObject = new Child(10, 20); ... Read More

Length of a JavaScript Object

AmitDiwan
Updated on 14-Sep-2020 08:30:25

201 Views

Let’s say the following is our Student object −var studentObject = new Object(); studentObject["studentFirstName"] = "John"; studentObject["studentLastName"] = "Doe"; studentObject["studentAge"] = 22; studentObject["studentCountryName"] = "US"; studentObject["studentCollegeName"] = "MIT"; studentObject["studentSubjectName"] = "JavaScript";Let’s find the length.You can use the concept of keys available in object and if the key is present then increment the counter variable and return the counter after completing the for loop.Examplevar studentObject = new Object(); studentObject["studentFirstName"] = "John"; studentObject["studentLastName"] = "Doe"; studentObject["studentAge"] = 22; studentObject["studentCountryName"] = "US"; studentObject["studentCollegeName"] = "MIT"; studentObject["studentSubjectName"] = "JavaScript"; Object.findLength = function (stObject) {    var counter = 0, k;    for (k in ... Read More

Combine Two Arrays into an Array of Objects in JavaScript

AmitDiwan
Updated on 14-Sep-2020 08:27:05

3K+ Views

Let’s say the following are our two arrays −var firstArray = ['John', 'David', 'Bob']; var secondArray = ['Mike', 'Sam', 'Carol'];To combine two arrays into an array of objects, use map() from JavaScript.Examplevar firstArray = ['John', 'David', 'Bob']; var secondArray = ['Mike', 'Sam', 'Carol']; var arrayOfObject = firstArray.map(function (value, index){    return [value, secondArray[index]] }); console.log("The First Array="); console.log(firstArray); console.log("The Second Array="); console.log(secondArray); console.log("The mix Of array object="); console.log(arrayOfObject);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo190.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo190.js The First Array= [ 'John', ... Read More

Make TextField Empty After Button Click in JavaScript

AmitDiwan
Updated on 14-Sep-2020 08:24:19

2K+ Views

For this, you can use onclick=”yourFunctionName()”, and −document.getElementById(“”).value=’’Example Live Demo Document ClearInputText    function clearTheTextField(){       console.log("The text box       value="+document.getElementById('txtBox').value)       document.getElementById('txtBox').value = '';    } To run the above program, just save the file name anyName.html(index.html) and right click on the file and select the option open with live server in VS Code editor.OutputThis will produce the following output −Now enter some value into the text box and click the button ClearInputText.After entering the value, clicking the button.This will produce the following output −

Advertisements