Showing posts with label API. Show all posts
Showing posts with label API. Show all posts

Thursday, January 6, 2022

Java String startsWith() Examples

1. Overview

In this article, You'll learn how to Test if this string starts with the specified prefix

Method startsWith() is case-sensitive.

String API is added with a method startsWith() that returns true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise. Note also that true will be returned if the argument is an empty string or is equal to this String object as determined by the equals(Object) method.

This method works exactly opposite to the String endsWith() method.

Java String startsWith() Examples

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)

Monday, December 21, 2020

Java String subSequence() Examples - Print subSequence in String

1. Overview


In this article, You'll learn how to get the subsequence from a String. This looks similar to the java string substring() method but the substring method returns a new String but subSequence method returns CharSequence rather than String.

Java String subSequence() Examples - Print subSequence in String

Sunday, December 6, 2020

Java String split() Examples on How To Split a String With Different Delimiters

1. Overview


As part of String API features series, You'll learn how to split a string in java and also how to convert a String into String array for a given delimiter

This concept looks easy but quite a little tricky. You'll understand this concept clearly after seeing all the examples on the split() method.

The string split() method breaks a given string around matches of the given regular expression.
Java String split() Examples on How To Split a String

Tuesday, July 21, 2020

Java String replaceFirst() Example

Java String replaceFirst()

1. Overview


In this String API Methods series, You'll learn replaceFirst() method of String class.

Replaces the first substring of this string that matches the given regular expression with the given replacement.

This method is mostly useful when you want to do the changes only for the first found value and not for all values. But, replace() method replaces for all matches with the given string. You must be careful and choose the right one for your use-case.

Java String replaceFirst() Example

Thursday, July 16, 2020

Java String codePointCount()

1. Overview


In this String API Series, You'll learn how to get the count of the codepoints in the string for a given text range.

In the previous article, we have discussed codePointAt() method which is to get the codepoint at the given index.

codePointCount() returns the number of Unicode code points in the specified text range of this String. The text range begins at the specified beginIndex and extends to the char at index endIndex - 1. Thus the length (in chars) of the text range is endIndex-beginIndex. Unpaired surrogates within the text range count as one code point each.

Let us jump into codePointCount() method syntax and example programs.

Java String codePointCount()

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

Sunday, January 12, 2020

Java 8 Spliterator API - Working Examples

1. Overview

In this tutorial, We'll be talking about Spliterator introduced in the new Java 8 (JDK8). Spliterator is an interface that is designed for bulk or huge datasets and provides the best performance.

Spliterator takes the data from the source and traverses it. Finally, it divides the data into partitions. Source data can be array, any collection or IO Channel such as files.

Spliterator does the partition using trySplit() method as another Spliterator. By partitioning, Operations can be performed parallel.

A Spliterator also reports a set of characteristics() of its structure, source, and elements from among ORDERED, DISTINCT, SORTED, SIZED, NONNULL, IMMUTABLE, CONCURRENT, and SUBSIZED. These may be employed by Spliterator clients to control, specialize or simplify computation.

For example, a Spliterator for a Collection would report SIZED, a Spliterator for a Set would report DISTINCT, and a Spliterator for a SortedSet would also report SORTED.

We will see its syntax and how Spliterator can be instantiated?
This has many useful methods and explanations for each method with example programs.

Java 8 Spliterator API - Working Examples


Thursday, July 11, 2019

Java 8 - Working with IntSupplier

1. Overview


In this tutorial, We'll learn how to use IntSupplier Functional Interface and part of package java.util.function which has been introduced in java 8.

IntSupplier represents a supplier of int-valued results. This is the int-producing primitive specialization of Supplier.

As we discussed in the previous article on "Working with Supplier Functional Interface", IntSupplier comes under Supplier's category and does not take any argument.

IntSupplier has only one functional method that is getAsInt().

Java 8 - Working with IntSupplier


When we expect an int primitive value rather than Integer wrapper then we should not use directly Supplier FI. Because it does autoboxing and leads to performance issues.

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