


What are the methods in Java to determine whether a string contains a specific string?
N ways to determine whether a string contains a certain substring:
1, startsWith()
method
2. contains()
method
3.indexOf
method
startsWith() method
There are two variants of this method that detect whether a string starts with a specified prefix.
The syntax defined by this method is as follows:
public boolean startsWith(String prefix, int toffset) or public boolean startsWith(String prefix)
Parameter description:
prefix
– The prefix to be matched.
toffset
– Where to start looking for the string.
Return value:
true and false
Related video tutorial recommendation: java learning video
Example:
import java.io.*; public class Test{ public static void main(String args[]){ String Str = new String("Welcome to Yiibai.com"); System.out.print("Return Value :" ); System.out.println(Str.startsWith("Welcome") ); System.out.print("Return Value :" ); System.out.println(Str.startsWith("Tutorials") ); System.out.print("Return Value :" ); System.out.println(Str.startsWith("Yiibai", 11) ); } }
contains method
java.lang.String.contains()
method returns true if and only if this string contains the specified sequence of char values.
Return value:
true and false
Examples are as follows:
public static void main(String[] args) { String str = "abc"; boolean status = str.contains("a"); if(status){ System.out.println("包含"); }else{ System.out.println("不包含"); } }
indexOf method
java.lang.String.indexOf()
The purpose is to find the position of a word in a string, and also to determine whether a string contains a certain character.
Return value:
int
The example is as follows:
public static void main(String[] args) { String str1 = "abcdefg"; int result1 = str1.indexOf("ab"); if(result1 != -1){ System.out.println("字符串str中包含子串“ab”"+result1); }else{ System.out.println("字符串str中不包含子串“ab”"+result1); } }
For more related tutorials, please visit: java introductory learning
The above is the detailed content of What are the methods in Java to determine whether a string contains a specific string?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version
SublimeText3 Linux latest version
