DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Functions for Strings in Java

Functions for Strings in Java

In this tutorial, you will learn how to make better use of built-in functions for Strings in Java to program more quickly, effectively, and aesthetically.

Yuri Filatov user avatar by
Yuri Filatov
·
May. 10, 21 · Java Zone · Tutorial
Like (1)
Save
Tweet
5.22K Views

Join the DZone community and get the full member experience.

Join For Free

What Is a String?

Firstly, of course, we have to initialize our string. What is a string used for?

  • You want to look at your string as a line, not as a mass of symbols.
  • If you have a long text, you want to work with the words, not the letters.
  • If you have lots of information, you need functions that solve questions as quickly as possible.

Initializing a String

String line;

Or with appropriate length:

String line = new String[any length];

Getting a Line From Console

Scanner in = new Scanner(System.in);

String line = in.nextLine();

Getting a Position

If you need the position of any symbol, use indexOf(...)

It returns a numeric value (position) of a symbol (first if they are repeating) written in brackets.

int pos = line.indexOf('any symbol');

Remember, ' ' is for symbols, and " " is for String (mass of symbols).

Cut

When you get your position, you can cut your string.

For example, if you have line="Hello-World" and you want to get line="Hello World", you need a position of '-' and then you may cut it.

Functions

substring(...)

Here, in brackets (start position, end position); so you cut from 0 position to the position of '-'. Here, the position is 5. So newline = line.substring(0,5);

Then add the "tail" of our line ("World"). newline += line.substring(6, line.length());

length()

Length regulates the number of symbols in your line. So it can be used as the end position in the substring.

Equals(...)

If we want to compare two strings, we use equals(...). It returns a boolean variable, so the result can be true or false. It is mostly used with if statements.

Java
 




x


 
1
if (line.equals(newline)==true) {
2
    System.out.println("Your lines are equal");
3
}


isEmpty(...)

Checking for emptiness is very important in case you don't want to catch mistakes. It returns a boolean variable, so the result can be only true or false. Oftentimes, we use isEmpty(...) in if statements.

Java
 




xxxxxxxxxx
1


 
1
if (line.isEmpty()) {
2
    System.out.println("Your line is empty");
3
}


matches()

If you want to compare some parts (using patterns), rather than entire lines, use matches(). Patterns are regular expressions. matches() return a boolean variable, so mostly used with if statements.

Java
 




xxxxxxxxxx
1


 
1
if (line.matches ("\\d{3}") {
2
    System.out.println("Your line contains 3 numbers");
3
}


Further Reading: More Regular Expressions

Strings Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Your Old Laptop Is Your New Database Server
  • Chopping the Monolith
  • Data Visualization of Healthcare Expenses by Country Using Web Scraping in Python
  • How To Use Cluster Mesh for Multi-Region Kubernetes Pod Communication

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo