Identify If a String Is a Number in C#

vanithasree
Updated on 02-Apr-2020 11:06:44

323 Views

Let us say our string is −string str = "3456";Now, to check whether the entered string is a number or not −str.All(c => char.IsDigit(c))The above returns true if the string is a number, else false.Here is the complete code −Example Live Demousing System; using System.Linq; namespace Demo {    public class MyApplication {       public static void Main(string[] args) {          string str = "3456";          // checking if string is a number or not          Console.WriteLine(str.All(c => char.IsDigit(c)));       }    } }OutputTrue

Iterate Over a HashList in Java

Arjun Thakur
Updated on 02-Apr-2020 11:03:52

2K+ Views

Declare a list and add elements −var products = new List < string > (); // adding elements products.Add("Belts"); products.Add("T-Shirt"); products.Add("Trousers");Use a loop to iterate −foreach(var p in products) {    Console.WriteLine(p); }ExampleLet us see the complete example −using System; using System.Collections.Generic; public class Demo {    public static void Main(string[] args) {       var products = new List < string > ();       products.Add("Belts");       products.Add("T-Shirt");       products.Add("Trousers");       Console.WriteLine("Our list....");       foreach(var p in products) {          Console.WriteLine(p);       }    } }

Implement Encapsulation Concept in JShell in Java 9

raja
Updated on 02-Apr-2020 09:44:51

150 Views

Java Shell (simply JShell) is a REPL interactive tool for learning the Java and prototyping Java code. It evaluates declarations, statements, and expressions as entered and immediately prints out the result and runs from the command-line.Encapsulation is an important concept in Java to make sure that "sensitive" data has been hidden from users. To achieve this, we must declare a class variable as private and provides public access to get and set methods and update the value of a private variable.In the below code snippet, we have implemented the Encapsulation concept for Employee class.jshell> class Employee { ...>       private String firstName; ...>     ... Read More

Sorting Field Value by First Name for MongoDB

AmitDiwan
Updated on 02-Apr-2020 08:37:20

368 Views

To sort values, use sort() in MongoDB. Let us first create a collection with documents −> db.demo365.insertOne({"FirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d5b6d0ada61456dc936f") } > db.demo365.insertOne({"FirstName":"Adam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d5bad0ada61456dc9370") } > db.demo365.insertOne({"FirstName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d5bed0ada61456dc9371") } > db.demo365.insertOne({"FirstName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d5c0d0ada61456dc9372") }Display all documents from a collection with the help of find() method −> db.demo365.find();This will produce the following output −{ "_id" : ObjectId("5e57d5b6d0ada61456dc936f"), "FirstName" : "Chris" } { "_id" : ObjectId("5e57d5bad0ada61456dc9370"), "FirstName" : "Adam" } { "_id" : ... Read More

MongoDB Query to Find Records with Keys Containing Dots

AmitDiwan
Updated on 02-Apr-2020 08:35:48

821 Views

For this, use $addFields. In that, use the $objectToArray to get the data in the form of keys and values. Use $filter with $indexOfBytes to check if there are any keys with. inside it.Let us first create a collection with documents −> db.demo364.insertOne( ...   { ...       "details" : { ...          "details1.otherdetails.Name" : {"FirstName":"Chris" } ...       } ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d485d0ada61456dc936d") } > db.demo364.insertOne( ...    { ...       "details" : { ...          "details1.otherdetails.Name" ... Read More

Use deleteOne Function in MongoDB

AmitDiwan
Updated on 02-Apr-2020 08:31:01

289 Views

The deleteOne() function in MongoDB deletes at most one matching document from a collection. Let us first create a collection with documents −> db.demo363.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d2c3d0ada61456dc9369") } > db.demo363.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d2c7d0ada61456dc936a") } > db.demo363.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d2cad0ada61456dc936b") } > db.demo363.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d2d1d0ada61456dc936c") }Display all documents from a collection with the help of find() method −> db.demo363.find();This will produce the following output −{ "_id" : ObjectId("5e57d2c3d0ada61456dc9369"), "Name" : "Chris" } { "_id" : ObjectId("5e57d2c7d0ada61456dc936a"), ... Read More

Fetch Multiple Documents in MongoDB Query with OR Condition

AmitDiwan
Updated on 02-Apr-2020 08:26:29

181 Views

Let us create a collection with documents −> db.demo362.insertOne({"ClientName":"John", "ClientProject":"School Management System"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a77454a481fef8ec7a1c") } > db.demo362.insertOne({"ClientName":"David", "ClientProject":"Library Management System"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a78454a481fef8ec7a1d") } > db.demo362.insertOne({"ClientName":"Mike", "ClientProject":"Event Tracker"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a7a054a481fef8ec7a1e") } > db.demo362.insertOne({"ClientName":"Carol", "ClientProject":"Hospital Management System"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a7b754a481fef8ec7a1f") }Display all documents from a collection with the help of find() method −> db.demo362.find();This will produce the following output −{ "_id" : ObjectId("5e56a77454a481fef8ec7a1c"), "ClientName" : "John", "ClientProject" : "School Management System" } { "_id" : ... Read More

Using Object as Key for Finding Values in MongoDB

AmitDiwan
Updated on 02-Apr-2020 08:24:31

1K+ Views

For finding values, use find() in MongoDB. Under that, use dot notation. Let us create a collection with documents −> db.demo361.insertOne({"details":{"FirstName":"Chris", "LastName":"Brown"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a61f54a481fef8ec7a19") } > db.demo361.insertOne({"details":{"FirstName":"David", "LastName":"Miller"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a62854a481fef8ec7a1a") } > db.demo361.insertOne({"details":{"FirstName":"John", "LastName":"Doe"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a63654a481fef8ec7a1b") }Display all documents from a collection with the help of find() method −> db.demo361.find();This will produce the following output −{ "_id" : ObjectId("5e56a61f54a481fef8ec7a19"), "details" : { "FirstName" : "Chris", "LastName" : "Brown" } } { "_id" : ObjectId("5e56a62854a481fef8ec7a1a"), "details" : { "FirstName" : ... Read More

Find Current and Previous Documents in MongoDB

AmitDiwan
Updated on 02-Apr-2020 08:22:14

668 Views

To get the current and previous documents, use sort(). Since we need only 2 documents, therefore, use LIMIT(2).Let us create a collection with documents −> db.demo360.insertOne({id:101, "Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a10c54a481fef8ec7a15") } > db.demo360.insertOne({id:102, "Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a11254a481fef8ec7a16") } > db.demo360.insertOne({id:103, "Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a11a54a481fef8ec7a17") } > db.demo360.insertOne({id:104, "Name":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a11f54a481fef8ec7a18") }Display all documents from a collection with the help of find() method −> db.demo360.find();This will produce the following output −{ "_id" : ObjectId("5e56a10c54a481fef8ec7a15"), "id" : ... Read More

MongoDB Query to Concatenate Values of Array with Other Fields

AmitDiwan
Updated on 02-Apr-2020 08:20:59

1K+ Views

To concatenate in MongoDB, use $concat in $project. Let us create a collection with documents −> db.demo359.insertOne( ...    { ... ...       Name1: "Chris", ...       Name2: "David", ...       Subjects: ["MySQL", "MongoDB", "Java"] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5694cdf8647eb59e5620d0") }Display all documents from a collection with the help of find() method −> db.demo359.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e5694cdf8647eb59e5620d0"),    "Name1" : "Chris",    "Name2" : "David",    "Subjects" : [       "MySQL",       "MongoDB",   ... Read More

Advertisements