AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

Java Strings


Java Tutorial » Java Strings

Strings, widely used in Java programming, are used for storing characters surrounded by double quotes.

In the below example, we create a variable of type string and then we assign it a value:
Example:
public class Main {
  public static void main(String[] args) {
    String greeting = "Hey, Hey man.";
  System.out.println(greeting);
  }
}
Output:
Hey, Hey man.

 


String Length


The length() method will return the number of characters that are contained in the string.

Example:
public class Main {
  public static void main(String[] args) {
    String my_txt = "agernic tutorial contain pretty good explanations";
    System.out.println("Text length of the string is: " + my_txt.length());
  }
}
Output:
Text length of the string is: 49


String Concatenation


Combine more strings using + operator is called concatenation:

This will returns a new string as string1 with string2 added to it at the end.

Example: use the + method
public class Main {
  public static void main(String args[]) {
    String Name = "Chris";
    String SurName = "Langford";
  System.out.println(Name + " " + SurName);
  }
}
Output:
Chris Langford
Note: we have added an empty text (" ") to create a space between mames.


We use the concat() method to concatenate two strings

Example: concat() method
public class Main {
  public static void main(String[] args) {
    String Name = "Chrys ";
    String SurName = "Store";
    System.out.println(Name.concat(SurName));
  }
}
Output:
Chrys Store


Finding a Character or character sequence in a String


In a specified text indexOf() method returns the index (position) in a string.

Example:
public class Main {
  public static void main(String[] args) {
    String my_txt = "please locate me where is the position of the word: me";
    System.out.println(my_txt.indexOf("me"));
  }
}
Output:
14




Java String to init, split, length, methods, format, array, contains, compare, equals, concatenation Java Strings imutable, substring, list, compare,
Java String - java

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Upload Image, display, edit and delete i...
Login Form with Image background...
How to Create an Image with Transparent ...
Portfolio Gallery Website with filtering...
Simple pagination script PHP MySQLi...
Center Image in div...
Image Hover Overlay Fade...
Sticky image / element / div on scroll...
Responsive images...
Create rounded image in HTML CSS...
Add border around image...
Position Text Over an Image HTML CSS res...
Create a Slideshow Images Gallery...
Create a Sticky Sidebar...
Search bar using CSS HTML...
Shrink Navigation Menu on Scroll...
How to Create Accordion HTML Templates...
Dropdown menu in the navigation bar...
Responsive Top Navigation Bar with mobil...
Split horizontal navigation bar...