Posts

Showing posts from March, 2021

Go Language - Validate Email Address Example

Image
Email validation is required in nearly every application that has user registration in place. An email has its own structure, and before using it, we need to validate it. In Go Language, email validation can be performed by using the standard library function mail.ParseAddress() or regular expression. Solution 1: Using the standard library function mail.ParseAddress() package main import ( "fmt" "net/mail" ) // Main function func main() { fmt.Println(ValidateEmail( "knowledgefactory-gmail.com" )) fmt.Println(ValidateEmail( "knowledgefactory4u@gmail.com" )) fmt.Println(ValidateEmail( "knowledge-factory4u@gmail.com" )) } func ValidateEmail(email string ) bool { _, err := mail.ParseAddress(email) return err == nil } Output: Solution 2: Using regular expression package main import ( "fmt" "regexp" ) // Main function func main() { fmt.Println(ValidateEmail( "knowledge...

How to make the first letter of a string lowercase in Go Language?

Image
How to make the first letter of a string lowercase, but not change the case of any of the other letters? For example:   "Go language is awesome" → "go language is awesome" "Hello world" → "hello world" Solution 1: package main import ( "unicode" "unicode/utf8" ) // Main function func main() { str := "Go language is awesome" println(firstToLower(str)) } func firstToLower(s string ) string { if len(s) > 0 { r, size := utf8.DecodeRuneInString(s) if r != utf8.RuneError || size > 1 { lower := unicode.ToLower(r) if lower != r { s = string(lower) + s[size:] } } } return s } Output: Solution 2: Easy package main import "strings" // Main function func main() { str := "Go language is awesome" str = strings.ToLower(string(str[ 0 ])) + str[ 1 :] println(str) } Output: Solutio...

How to make the first letter of a string uppercase in Go Language?

Image
How to make the first letter of a string uppercase, but not change the case of any of the other letters? For Example: "go language is awesome" → "Go language is awesome" "hello world" → "Hello world" Solution 1: Easy package main import ( "unicode" ) // Main function func main() { str := "go language is awesome" runes := []rune(str) if len(runes) > 0 { runes[ 0 ] = unicode.ToUpper(runes[ 0 ]) } println(string(runes)) } Output: Solution 2: Easy package main import "strings" // Main function func main() { str := "go language is awesome" str = strings.ToUpper(string(str[ 0 ])) + str[ 1 :] println(str) } Output: Solution 3: Normal package main import ( "unicode" "unicode/utf8" ) // Main function func main() { str := "go language is awesome" println(firstToUpper(str)) } func firstToUpper(s string ) string { ...

String operations - Learn Java 8 with examples

Split a String import java.util.Arrays ; import java.util.stream.Collectors ; public class Main { public static void main ( String [] args) { String str = "I love my country" ; Arrays . stream ( str .split( " " )) .collect( Collectors . toList ()) .forEach( System . out ::println); } } Declare String which we are going to split. The stream here is created from an existing array using the Arrays.stream() method . All array elements are converted to stream elements. Here, split() method converts String based on whitespace and returns array of String. You need to pass a special object to the method collect(). This object reads all the data from the stream, converts it to a specific collection, and returns it.  toList() method Returns a Collector that accumulates the input elements into a new List. Finally, Iterate over List and print each elements to the console. Console Output:  I love my country More examples, ...

Java 8 Streams - How to find the duplicate elements from a Collection - Multiple ways

Example 1: Using Collections.frequency() To find frequency of a particular element we can use the frequency() method: import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; public class DriverClass { public static void main( String [] args) { List < String > obj = Arrays.asList( "ant" , "1" , "cat" , "ant" , "we" , "java" , "cat" ); obj.stream().filter(i -> Collections. frequency(obj, i) > 1 ).collect(Collectors.toSet()) .forEach(System.out :: println); } } Output: ant cat Example 2: Using Collectors.groupingBy() We can also use the Collectors.groupingBy() method to count the frequency of elements present in a stream.  import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.Function; import java.ut...

Vue.js + Python + MongoDB CRUD example

Image
Hello everyone, today we will learn how to develop a  web application that is a basic User Management Application using MongoDB , Vue.js , Python , and Flask . GitHub repository link is provided at the end of this tutorial. You can download the source code. More Related Topics, React.js + Python + MongoDB CRUD application Python + MongoDB + Vue.js CRUD Application  Angular10 + Python + MongoDB CRUD application Python-Create a CRUD Restful Service API using FLASK and MYSQL Python-Simple CRUD Web App with Python, Flask, and Mysql Python with MongoDB Atlas - CRUD After completing this tutorial what we will build? We will build a full-stack web application that is a basic User Management Application with CRUD features:       • Create User     • List User     • Update User     • Delete User User Interfaces -Retrieve all Users: -Add a User: -Update User: We divided this tutorial into two parts.  PART 1 - Rest APIs Development using P...