Eyestech
  • Home
  • Apps and Services
    • IPhone
    • Android
  • Reviews
  • Tech News
  • Contact us
Instagram
Eyestech
Eyestech
  • Home
  • Apps and Services
    • IPhone
    • Android
  • Reviews
  • Tech News
  • Contact us
  • java
  • programming

Strings in java: All the Methods and Definition

  • July 22, 2020
  • java hub
Total
0
Shares
0
0
0

In Today’s world data is everywhere. Without data, nothing seems to happen from everyday life to business everything in the form on the top of data. One type of data that is from held the beginning is known to be text. In Java, we can store text with the help of the String Class. The
string class is an array of characters.
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared

Let us begin with the basics

Syntax

String str = new String(“Hello”);
String strNew = “World”;
String str1 = “”; // empty string
String str2 = null;

Note: Know the difference between the empty string and the null value.

An empty string is initialized with the array without any values but the null string is not even initialized

For example

System.out.println(str1.length()); // gives 0 as its empty
System.out.println(str2.length()); // throws NullPointerException since the Object is not initialized

PS : int length() returns the length of the string

Concating strings are most used operators on the string

Example

str = str.concat(strNew); // HelloWorld String -- class provides concat
api for this use case
str1 = str + " " + strNew; // HelloWorld World -- you can also achieve
with this using + operator too

Some important methods of Strings are :

int indexOf(int ch)

Returns the index within this string of the first occurrence of the specified character. It returns -1 if the specified character is not found in the String

str = "Hello world!";
System.out.println(str.indexOf('l')); // 

Overloaded methods

  • indexOf(int ch, int fromIndex)
  • indexOf(String str)
  • indexOf(String str, int fromIndex)

Similar methods

lastIndexOf(int ch)
lastIndexOf(int ch, int fromIndex)
lastIndexOf(String str)
lastIndexOf(String str, int fromIndex)
Note: if the fromIndex is specified then it searches from that specific index

char charAt(int index) 

Returns the char value at the specified index.

str = "Hello world!"; 
System.out.println(str.charAt(4)); // o 

Note: if the specified index not in the range 0 <= index < str.length() then it throws StringIndexOutOfBoundsException 

boolean contains(CharSequence s) 

Returns true if and only if this string contains the specified sequence of char values.

str = "Hello world!"; 
System.out.println(str.contains("ello")); // true

int compareTo(String anotherString) 

Compares two strings lexicographically. Return the first character difference other wise 0

System.out.println("Hello".compareTo("Hello")); //0
System.out.println("hello".compareTo("Hello")); //32 
System.out.println("Hello".compareTo("hello")); //-32 

Similar methods 

compareToIgnoreCase(String str) // it does the same but ignores the case difference 

boolean startsWith(String prefix) 

Tests if this string starts with the specified prefix.

str = "Hello world!"; 
System.out.println(str.startsWith("ello")); // false
System.out.println(str.startsWith("Hel")); // true 

Overloaded methods 

startsWith(String prefix, int offset) //checks from the offset 

boolean endsWith(String suffix) 

Tests if this string ends with the specified suffix.

 str = "Hello world!";
 System.out.println(str.endsWith("ello")); // false
 System.out.println(str.endsWith("d!")); // true 

String substring(int beginIndex) 

Returns a new string that is a substring of this string.

str = "Hello World!"; 
System.out.println(str.substring(2)); //llo World! 
System.out.println(str.substring(2, 5)); //llo 

Overloaded methods 

substring(int beginIndex, int endIndex) 

char[] toCharArray() 

Converts this string to a new character array. 

String str = "Hello World!";
char[] characters = str.toCharArray();
System.out.println(characters); //Hello World! 

String toLowerCase() 

Converts all of the characters in this String to lower case using the rules of the default locale.

String str = "Hello World!"; 
System.out.println(str.toLowerCase()); //hello world! 

Note: We can also change the default Locale using this API. 

String toUpperCase() 

Converts all of the characters in this String to upper case using the rules of the default locale. 

String str = "Hello World!";
System.out.println(str.toUpperCase()); //HELLO WORLD! 

Note: We can also change the default Local using this API. 

String trim() 

It deletes the leading and trailing spaces in the string. 

String str = " _ Hello World! _ ";
System.out.println(str.trim()); //_ Hello World! _ 

String replace(char oldChar, char newChar) 

Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar. 

String str = "Hello World!";
System.out.println(str.replace('l','i')); //Heiio Worid! 

Similar methods 

replace(CharSequence target, CharSequence replacement)

replaceAll(String regex, String replacement)

replaceFirst(String regex, String replacement) 

String[] split(String regex) 

Splits this string around matches of the given regular expression. It returns an array of Strings. 

String str = "Hello World! This is the start of the every experiment"; 
String[] words = str.split("\\s");
System.out.println(Arrays.toString(words)); // [Hello, World!, This, is, the, start, of, the, every, experiment] 

Overloaded methods 

split(String regex, int limit) 

Converting Other data types to String 

Some times we need to convert any data type to String. For those use cases, we have the following methods. Check that out 

  • String valueOf(boolean b)
  • String valueOf(char c)
  • String valueOf(char[] data)
  • String valueOf(char[] data, int offset, int count)
  • String valueOf(double d)
  • String valueOf(float f)
  • String valueOf(int i)
  • String valueOf(long l)
  • String valueOf(Object obj) 

Now you got a basic idea about the strings. Do check this for more methods

Total
0
Shares
Share 0
Tweet 0
Pin it 0
Avatar
java hub

Previous Article
  • Mobiles

Motorola one Fusion plus: The best midrange for Indians

  • July 21, 2020
  • Muskan Agarwal
View Post
Next Article
  • Apps and Services

12 Best Siri Shortcuts Apps for Power Users

  • July 23, 2020
  • Shivani Verma
View Post

Subscribe

Subscribe now to our newsletter

You May Also Like
Enable Java in Chrome
View Post
  • java

How to Enable Java in Chrome [2021]

  • Nusrath
  • February 23, 2021
View Post
  • programming

Best Ide for Machine Learning and AI (2020)

  • user
  • November 2, 2020
java
View Post
  • programming

7 Best Java Tips and Tricks of 2020

  • java hub
  • June 29, 2020
java ide's
View Post
  • programming

5 Best Java ide’s in 2020: Get the best development environment

  • java hub
  • June 20, 2020

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Featured Posts
  • 1
    6 Best Fitness Apps for Android and IOS [2021]
    • April 18, 2021
  • 2
    Best Food Delivery Apps In India of 2021
    • April 13, 2021
  • 3
    5 BEST WORKOUT APPS FOR APPLE WATCH
    • April 13, 2021
  • PANDA HELPER APP 4
    PANDA HELPER APP: HOW TO INSTALL
    • April 11, 2021
  • 5
    Redmi Earbuds S Review [2021]
    • April 11, 2021

Subscribe

Subscribe now to our newsletter

Eyestech
Juggernaut of positrons

Input your search keywords and press Enter.