Categories
Interview java String

count number of words in string using java

In this post, we will learn about count number of words in string using java

In below example we using split method to count number of words in a sentence.

If String have more than one space consecutively, then we need to replace with one string using below code

sentence.replaceAll("\\s+", " ").trim();
Syntax
properString.split(" ");
Example using split
public class CountWords {

	public static void main(String[] args) {

		try {
			String sentence = "    The pen    is mightier than the sword . ";
			String properString = sentence.replaceAll("\\s+", " ").trim();
			String[] split = properString.split(" ");
			System.out.println("The sentence have " + split.length + " words");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
Output
The sentence have 8 words
Example using StringTokenizer
import java.util.StringTokenizer;

public class CountWords {

	public static void main(String[] args) {
		try {
			String sentence = "    The pen    is mightier than the sword . ";
			StringTokenizer tokens = new StringTokenizer(sentence);
			System.out.println("The sentence have " + tokens.countTokens() + " words");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
Output
The sentence have 8 words
Github

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/strings/CountWordsUsingSplit.java

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/strings/CountWordsUsingTokenzier.java

Related Articles

find the first occurrence of a character in string using java

Categories
java String

find the first occurrence of a character in string using java

In this post, we will learn to find the first occurrence of a character in string using java

As like in above image we are going to take String “BeginnersBug” for our example.

This array starts from 0 index to 11.

In this example, we are going to find the first occurrence of character ‘e’. which is 1

Note: that indexOf is case sensitive. It will return -1 if the character is not found

Syntax
	int indexOf = s.indexOf('e');
Example
public class FirstOccurance {

	public static void main(String[] args) {
		try {
			String s = "BeginnersBug";
			int indexOf = s.indexOf('e');
			System.out.println("The first occurance of e is " + indexOf);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
Example
The first occurance of e is 1
Github

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/strings/FirstOccurance.java

Related Articles

find the last index of a character in string using java

Categories
java String

find the last index of a character in string using java

In this post, we will learn to find the last index of a character in string using java

As like in above image we are going to take String “BeginnersBug” for our example.

This array starts from 0 index to 11.

In this example, we are going to find the last occurrence of character ‘B’. which is 9

Note: that lastIndexOf is case sensitive. It will return -1 if the character is not found

Syntax
s.lastIndexOf('B');
Example
public class LastIndex {

	public static void main(String[] args) {
		try {
			String s = "BeginnersBug";
			int indexOf = s.lastIndexOf('B');
			System.out.println("The last index of B is " + indexOf);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
Output
The last index of B is 9
Github

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/strings/LastIndex.java

Related Articles

find the first occurrence of a character in string using java

Convert String to lowercase using java

Categories
java String

How to compare two strings in java

In this post, We will learn How to compare two strings in java

It might sounds easy. But we need to take care of few things while comparing two Strings

Avoid ==

If you are newbie to java, This might surprise you

while you use == to compare string it will not check the value of two strings

Instead of that it will check the reference of two Strings

"BeginnersBug" == new String("BeginnersBug") 
// Above check will return false 

In the above snippet even though both the String are equal it will return false because both are not same object

.equals() method

.equlas method will compare two strings with the value irrespective of its object

Even though if it is two different object it will compare the value of two string

"BeginnersBug".equals(new String("BeginnersBug")) 
// Above check will return true

In the above code snippet it will compare two string value and it will return true for the condition

. equalsIgnoreCase() method

equalsIgnoreCase() method will ignore the case while checking the value of two Strings

If you want to check two Strings irrespective of case sensitive you can use this method

"beginnersbug".equalsIgnoreCase(new String("BeginnersBug")) 
// Above method will return true

In the above code snippet equalsIgnoreCase method will ignore the case sensitive. So it is returning true for the statement

Example

public class CompareStringExample {
	public static void main(String[] args) {
		try {
			// Example 1 == Below check will failS
			if ("BeginnersBug" == new String("BeginnersBug")) {
				System.out.println("Example 1 : Both the strings are equal");
			} else {
				System.out.println("Example 1 : Both the strings are not equal");
			}

			// Example 2 .equals()
			if ("BeginnersBug".equals(new String("BeginnersBug"))) {
				System.out.println("Example 2 : Both the strings are equal");
			} else {
				System.out.println("Example 2 : Both the strings are not equal");
			}

			// Example 3 .equalsIgnoreCase()
			if ("beginnersbug".equalsIgnoreCase(new String("BeginnersBug"))) {
				System.out.println("Example 3 : Both the strings are equal");
			} else {
				System.out.println("Example 3 : Both the strings are not equal");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
Output
Example 1 : Both the strings are not equal
Example 2 : Both the strings are equal
Example 3 : Both the strings are equal
Reference

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#equalsIgnoreCase(java.lang.String)

Related Articles

Convert String to lowercase using java

Print a String in java with example

Categories
java String

Convert String to lowercase using java

Convert String to lowercase using java

In this example we will learn to convert a String to lowercase using java

Syntax

<span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

Example

public class StringLowerCase {

  public static void main(String[] args) {
    String s="A guy from New WORLD ";
    // Below line will convert your string to lower case
    String lowerCase=s.toLowerCase();
    System.out.println(lowerCase);
  }
}

Output

a guy from new world

Reference

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#toLowerCase()