Multiply a Given Number by 2 Using Bitwise Operators in C#

Ankith Reddy
Updated on 27-Jun-2020 06:43:29

4K+ Views

A number can be multiplied by 2 using bitwise operators. This is done by using the left shift operator and shifting the bits left by 1. This results in double the previous number.A program that demonstrates multiplication of a number by 2 using bitwise operators is given as follows.Example Live Demousing System; namespace BitwiseDemo {    class Example {       static void Main(string[] args) {          int num = 25, result;          result = num

Split a String with Dot in Java

Samual Sam
Updated on 27-Jun-2020 06:43:11

641 Views

Let’s say the following is our string.String str = "Java is a programming language. James Gosling developed it.";We will now see how to split a string using the split() method. Include the delimiter as the parameter.String[] strSplit = str.split("\.");Above, we have split the string with dot as you can see under the split methods parameter.The following is an example.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "Java is a programming language. James Gosling developed it.";       System.out.println("String: "+str);       String[] strSplit = str.split("\.");       ... Read More

Organised and Unorganised Sector in Simple Terms

Prasanna Kotamraju
Updated on 27-Jun-2020 06:42:26

406 Views

Organized and unorganized sectors are mainly differentiated based on employment conditions.Organized Sector − It is incorporated with the appropriate authority and follows the rules and regulations. It is mostly related to Government or some large-scale operations. As these organizations strictly follow government rules they adhere to acts like Factories Act, Bonus Act, PF Act, Minimum Wages Act etc. Workers are paid remuneration as monthly salary regularly and there will be job security.Unorganized Sector − The unorganized sector is the one which is not incorporated into the government. Small-scale operations, petty trade, private business, domestic workers and construction workers etc. come ... Read More

Line Separator in Java

karthikeya Boyini
Updated on 27-Jun-2020 06:40:15

3K+ Views

Strings have no newlines. We can form them into two lines by concatenating a newline string. Use System lineSeparator to get a platform-dependent newline string.The following is an example.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "one" + System.lineSeparator() + "two";       System.out.println(str);    } }Outputone twoLet us see another example. On Linux based system, the program will work correctly.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = System.lineSeparator();       System.out.println((int) str.charAt(0));    } }Output10

Left Pad a String with a Specified String in Java

Samual Sam
Updated on 27-Jun-2020 06:39:31

208 Views

The following is our string.String str = "Jack";Now take a StringBuilder object.StringBuilder strBuilder = new StringBuilder();Perform left padding and extend the string length to 20. The string that will be padded comes on the left.while (strBuilder.length() + str.length() < 20) { strBuilder.append("demo"); }The following is an example wherein we have left padded a string with another string “demo”Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "Jack";       StringBuilder strBuilder = new StringBuilder();       // left padding with a string       while (strBuilder.length() + str.length() ... Read More

Display Month in MMM Format in Java

karthikeya Boyini
Updated on 27-Jun-2020 06:38:45

5K+ Views

The MMM format for months is the short name i.e. Jan, Feb, Mar, Apr, etc. Here, we will use the following.SimpleDateFormat("MMM");Let us see an example.// displaying month in MMM format SimpleDateFormat simpleformat = new SimpleDateFormat("MMM"); String strMonth= simpleformat.format(new Date()); System.out.println("Month in MMM format = "+strMonth);Above, we have used the SimpleDateFormat class, therefore the following package is imported.import java.text.SimpleDateFormat;The following is an example.Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo {    public static void main(String[] args) throws Exception {       // displaying current date and time       Calendar cal = Calendar.getInstance();   ... Read More

Display Month in MMMM Format in Java

Samual Sam
Updated on 27-Jun-2020 06:37:32

10K+ Views

The MMMM format for months is like entire month name: January, February, March, etc. We will use it like this.SimpleDateFormat("MMM");Let us see an example.// displaying month in MMMM format SimpleDateFormat simpleformat = new SimpleDateFormat("MMMM"); String strMonth= simpleformat.format(new Date()); System.out.println("Month in MMMM format = "+strMonth);Above, we have used the SimpleDateFormat class, therefore the following package is imported.import java.text.SimpleDateFormat;The following is an example.Example Live Demoimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo {    public static void main(String[] args) throws Exception {       // displaying current date and time       Calendar cal = Calendar.getInstance();     ... Read More

Remove Newline, Space, and Tab Characters from a String in Java

karthikeya Boyini
Updated on 27-Jun-2020 06:36:38

18K+ Views

To remove newline, space and tab characters from a string, replace them with empty as shown below.replaceAll("[\t ]", "");Above, the new line, tab, and space will get replaced with empty, since we have used replaceAll()The following is the complete example.Example Live Demopublic class Demo {    public static void main(String[] args) {       String originalStr = "Demo\tText";       System.out.println("Original String with tabs, spaces and newline: "+originalStr);       originalStr = originalStr.replaceAll("[\t ]", "");       System.out.println("String after removing tabs, spaces and new line: "+originalStr);    } }OutputOriginal String with tabs, spaces and newline: Demo Text ... Read More

Check If a String is Whitespace, Empty or Null in Java

Samual Sam
Updated on 27-Jun-2020 06:35:40

2K+ Views

Let’s say the following is our string.String myStr = "";Now, we will check whether the above string is whitespace, empty ("") or null.if(myStr != null && !myStr.isEmpty() && !myStr.trim().isEmpty()) {    System.out.println("String is not null or not empty or not whitespace"); } else {    System.out.println("String is null or empty or whitespace"); }The following is an example that checks for an empty string.Example Live Demopublic class Demo {    public static void main(String[] args) {       String myStr = "";       if(myStr != null && !myStr.isEmpty() && !myStr.trim().isEmpty()) {          System.out.println("String is not null ... Read More

Join Strings in Java

karthikeya Boyini
Updated on 27-Jun-2020 06:32:26

1K+ Views

To join strings in Java, use the String.join() method. The delimiter set as the first parameter in the method is copied for each element.Let’s say we want to join the strings “Demo” and “Text”. With that, we want to set a delimeter $. For that, use the join() method as shown below −String.join("$","Demo","Text");The following is an example.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = String.join("$","Demo","Text");       System.out.println("Joined strings: "+str);    } }OutputJoined strings: Demo$Text

Advertisements