Showing posts with label lang. Show all posts
Showing posts with label lang. Show all posts

Tuesday, November 16, 2021

Java String toCharArray() - How to convert string to char array?

Java String toCharArray():


The java string toCharArray() method converts this string into character array. It returns a newly created character array, its length is similar to this string and its contents are initialized with the characters of this string.

Returns a newly allocated character array whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.


Java String toCharArray() with example - Convert string to char - Internal


Syntax:
public char[] toCharArray​()

Sunday, November 14, 2021

Java - How To Compare Two Strings Lexicographically | String compareTo method works internally

Java - How To Compare Two Strings Lexicographically


Today, we will learn about comparing two strings lexicographically in Java. Many of you might be aware of how to compare two strings in java but not in a manner lexicographically. Lexicographically word looks somewhat new and weird. Do not worry, we will go step by step easily understandable way and how it works internally.

First, We will go through examples of how to compare two string values are the same/equal or not.

Compare Two Strings Lexicographicall

Saturday, November 13, 2021

Java String format() Examples

Java String format​() method

String format method is used to format the string in the specified format. This method can be used as String output format by passing multiple arguments because the second parameter is the variable-arguments(Var-Args). Recommend to read in-depth usage of Var-Args in Java. We can pass any type of object here such as String, Float, Double etc together. You will find the examples on each in this course.

This format method is introduced in java 1.5 version and it is overloaded and static methods

Java String format() Examples

Java String format​() method Syntax: 

// Takes default locale 
public static String format​(String format, Object... args)
//Takes passed locale instead of default locale value
public static String format​(Locale l, String format, Object... args)

Wednesday, June 10, 2020

Java String regionMatches​() Method Example

1. Java String regionMatches​() Overview


In this tutorial, We'll learn about Java String API regionMatches​() method to compare two substrings. In other words, comparing regions of two strings.

This method is very handy when we want to compare portions of two strings. Instead of comparing all contents of Strings.

In previous article, We've discussed on String matches() method.

Java String API regionMatches​() Method Example


1.1 regionMatches() Syntax


public boolean regionMatches​(int toffset, String other, int ooffset, int len)
public boolean regionMatches​(boolean ignoreCase, int toffset, String other, int ooffset, int len)


Fist variant does case sensitive comparison
Second variant has option to ignore the case. If true, it ignores case when comparing.

Java String API regionMatches​()


Friday, May 29, 2020

Java String offsetByCodePoints​() Method Example

1. Java String offsetByCodePoints​() Method Overview

offsetByCodePoints​() method returns the index within this String that is offset from the given index by codePointOffset code points.
Unpaired surrogates within the text range given by index and codePointOffset count as one code point each.

Java String API offsetByCodePoints​() Method Example

1.1 Syntax


Here is the full syntax for this method and part of java.lang package.

public int offsetByCodePoints​(int index, int codePointOffset)

Article on Java Package

Wednesday, April 8, 2020

Java String charAt() Method examples (Find Char At a Given Index)

1. Introduction

In this article, We'll learn how to find the character at an index with String charAt in java.

The Java String charAt(int index) method returns the character at the specified index in a string. The index value should be between 0 and (length of string-1). For example, s.charAt(0) would return the first character of the string represented by instance s. Java String charAt method throws IndexOutOfBoundsException if the index value passed in the charAt() method is a negative number or less than zero or greater than or equal to the length of the string (index<0|| index>=length()). IndexOutOfBoundsException means index if out of its range and it occurs at runtime.

Java String charAt() Examples 

Syntax:

public char charAt​(int index)


Java String charAt() Method example (Find Char At a Given Index)


We will show the example programs on the following use cases. 

1: Finding first, mid and last index of the given string.
2: IndexOutOfBoundsException when using charAt() method.
3: Printing String char by char using charAt() method.
4: Printing even numbers of index characters.
5: Printing odd number index characters
6: Counting number of occurrences of a character

Monday, June 3, 2019

Java String API replaceAll() Method Example

1. String replaceAll() Overview


In this tutorial, We'll learn about Java String API replaceAll() Method with Example (String.replaceAll).

As name "replaceAll" suggests, It replaces each substring of this string that matches the given regular expression with the given replacement. Refer the below syntax, it takes regex which is a regular expression pattern.

This method is a instance method which should be invoked on a string. If this string has a pattern then it replaces all matched pattern's with the second parameter value.

In the previous article,  discussed on String replace() method with examples.

Java String API replaceAll() Method Example


1.1 Syntax


public String replaceAll​(String regex, String replacement)

Friday, May 31, 2019

Java String API replace() Method Example

1. Java String replace() Overview


In tutorial, We'll learn about Java String replace() method and explanation with examples. replace() method is used to replace a character with another character in a String and this method returns a new string after replacing characters.

In previous tutorial, We've seen How to use Java 11 String API repeat() method.

Java String API replace() Method Example

1.1 Syntax


public String replace​(char oldChar, char newChar)
public String replace​(CharSequence target, CharSequence replacement)

replace() method is available in two variants. First variant takes two char's as input and replace a char by a new char. Where as in second variant, It takes two CharSequence as arguments and replace one CharSequence by new CharSequence.

Here, CharSequence means String, StringBuffer, StringBuilder. Any one of these can be passed as arguments to the second variant of replace().

Wednesday, May 29, 2019

Java 11 String API repeat​() Method Example

1. Java 11 String repeat​() Method Overview


In this tutorial, We'll learn about Java String API repeat​() Method with examples. repeat() method repeat​s the string given n number of times.

repeat() method works as it name suggests.

Java 11 String API repeat​() Method Example

1.1 Syntax

public String repeat​(int count)

This is a public method and can be accessed on string instance directly.

1.2 Parameters

count - number of times to repeat

If we pass count as 3 then string repeats 3 times.

Tuesday, May 28, 2019

Java String API matches​() Method Example

1. Java String API matches​(String regex) Overview

In this tutorial, We'll learn about Java String API matches​() Method with Example programs. And also will explain how this method works internally.

matches() method tells whether or not this string matches the given regular expression.

In simple words, First regular expression is a pattern which tells string should have only alphabetic's or numbers. This pattern can be anything or specified character set.

If the string fits into the specified regular expression pattern then matches() method returns true. Otherwise returns false.



Java String API matches​() Method Example

Java String API length() Method Example

1. Java String length() Overview

In this tutorial, We'll learn how to find the length of the String using Java String API Length() Method. In other words, Counting the number of characters in String.

Knowing the string length is very important in iterating the string.

Note: All the blank spaces in string are counted as character.

Java String API length() Method Example

Monday, May 27, 2019

Java String indexOf() Method Example

1. Overview

In this tutorial, We will learn about String indexOf() method with example on how to check whether the specified character is present in the input string or not.

Java String indexOf() Method Example

Java 12 String API indent​() Method Example

1. Overview

In this quick tutorial, We'll learn new indent() introduced in JDK 12 in Java.lang.String class.

Demonstrated with examples how to use indent() method.

Please read article on Java 12 new String API methods addition.


Will go through its internal code and how it works.

Java 12 String indent​() Method Example

Java String getChars()​ Method Example

1. Overview

In this quick tutorial, We'll learn about String getChars​ method with examples. As well will take a look at internally how getChars​ method implemented and works.

Java String getChars​ Method Example


2. getChars​

This method copies the characters from this string into the destination character array. Also getChars​() method copies only a specified range of characters into new char array.
Source String start index, end index and destination array start index can be configured.

Monday, May 13, 2019

Guide To Check Char Is In String In Java - indexOf(), lastIndexOf​()

1. Overview

In this tutorial, We will check the possible ways to check the given char or string in the given string. If the char is present then need to return it's index.

Throughout this post, we will be writing the examples on below string.

Check Char Is In String

String string = "find string";


indexof() and lastIndexOf methods return int value as index of the found character.

Please go through the previous post, how String contains() method works in java with examples. This post will give you understanding how indexof method is used internally.

Saturday, May 4, 2019

Guide on Java String getBytes​

1. Overview:

Encodes the current String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.

This method has 4 overloaded methods.

public byte[] getBytes()
public void getBytes​(int srcBegin, int srcEnd, byte[] dst, int dstBegin) --> This is @Deprecated
public byte[] getBytes​(String charsetName) throws UnsupportedEncodingException
public byte[] getBytes​(Charset charset)



Java String getBytes

Tuesday, April 2, 2019

String equalsIgnoreCase​ method in java with example - Internal Implementation

String equalsIgnoreCase​ method in java:

In this post, We will learn more about java.lang.String.equalsIgnoreCase​() method as following today.

Step 1) What equalsIgnoreCase​ method does or method description
Step 2) Syntax, Parameters, Return type
Step 3) Example 1:  Program to check equalsIgnoreCase​ method
Step 4) Example 2:  Program to find the string is present in the List
Step 5) Difference between equals and equalsIgnoreCase
Step 6) How equalsIgnoreCase​ works internally and implementation code.

Step 1) What equalsIgnoreCase​ method does or method description

equalsIgnoreCase​ method compares this string to the specified object ignoring case. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object ignoring case. It simply ignores the case whether it is lower case or upper case.

String equalsIgnoreCase​ method in java

For example:

Read How String equals methods works in java with example programs.

Two strings are considered equal ignoring case if they are of the same length and corresponding characters in the two strings are equal ignoring case.

Friday, March 22, 2019

String isEmpty method in java with example - Internal Implementation

String isEmpty method in java:

In this post, We will learn more about java.lang.String.isEmpty() method.

Step 1) What this method does or method description

Step 2) Syntax, Parameters, Return type
Step 3) Example 1:  program to check String is empty or not
Step 4) Example 2:  program to check if the string is null or "  " empty spaces
Step 5) How isEmpty works internally and implementation code.

Step 1: String isEmpty method description

Java String isEmpty() method checks whether a String is empty or not. This method returns true if the given string is empty, else it returns false. In other words you can say that this method returns true if the length of the string is 0.

This method is present in package java.lang.String and introduced isEmpty in java 1.6.



String isEmpty method in java


Step 2: Syntax

The following is the syntax for isEmpty method.

public boolean isEmpty()

Parameters:

Does not take any parameters.


Return type:

boolean. Returns true if length is 0, otherwise false.

Friday, March 15, 2019

String endsWith​ Method in Java With Example - Internal Implementation

String endsWith​ method in java:

Java String endsWith​ method is used to check the string is ending with the specified string or not. This method returns true if the string is ending with given string or suffix.
Returns false if not ending with the specified string.


String endsWith​ method in java


Syntax:

public boolean endsWith​(String suffix)


Tests if this string ends with the specified suffix.

Parameters:

suffix - the suffix. This string will be searched in the input string.

Wednesday, March 13, 2019

String equals() method in java with example - Internal Implementation

String equals() method in java with example:

This method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.


Java String equals()


Syntax:

public boolean equals​(Object anObject)