search
HomeJavajavaTutorialCommonly used string methods in development

Commonly used string methods in development

Jun 26, 2017 am 11:11 AM
javastringCommonly useddevelopmethod

The function of java strings can be said to be very powerful, and each of its methods is also very useful.

There are two commonly used string classes in java strings, namely the String class and the StringBuffer class. .

Sting class

Objects of String class are immutable.

Create String

String()
String(String str)
String(char value[]) //用字符数组生成一个串对象String(char value[], int offset, int count) //用字符数组value的offset位开始的count个字符,建立一个字符串对象

Commonly used methods

int length()
String toLowerCase()//返回当前串的小写String toUpperCase()//返回当前串的大写char[] toCharArray()//返回当前串的字符串数组String trim()//删除当前字符串的前部和后部空格并返回新串

Example:

String str = new String("    Hello world    ");
str.length();    //返回str的长度为11str.toLowerCase();//将str转换为小写, "    hello world    "str.toUpperCase();//将str转换为大写, "    HELLO WORLD    "char[] strChar = str.toCharArray();//将str转换为strChar字符数组str.trim();//删除前后的空格, "Hello world"

Comparative method

boolean regionMatches(int toffset, String str, int ooffset, int len)//比较从本串的toffset开始的len个字符和str从ooffset开始的len个字符是否一致, 一致则返回true(可用来检测字符换字符串在当前串中出现的次数)boolean regionMatches(boolean IgnoreCase, int toffset, String str, int ooffset, int len)//同上, IgnoreCase决定是否忽略大小写, IgnoreCase为true时忽略大小写String concat(String str)//返回当前字符串与str串连接后的新串int compareTo(String str)//比较字符串中相同位置的Unicode, 若两串相等返回0, 否则当前串大于str返回比较字符的差值int compareToIgnoreCase(String str)//忽略大小写比较, 同上boolean equals(Object anObj)//比较两个对象的值是否相等.这里比较两个字符串对象是否相等boolean equalsIgnoreCase(String anotherString)//忽略大小写, 比较两个字符串对象是否相等boolean startsWith(String prefix[, int toffset])//判断当前字符串从toffset开支是否以参数prefix开头, []中括号表示可省略boolean endsWidth(string prefix[, int toffset])//判断当前字符串从toffset开始是否以参数prefix结尾

Find method

//字符ch查找, 注意是字符int indexOf(int ch)//从前向后找第一个字符ch出现的位置, 未找到返回-1int indexOf(int ch, int fromIndex)//从fromIndex位置开始向后找第一个字符ch出现的位置, 未找到返回-1int lastIndexOf(int ch)//从后向前找第一个字符ch出现的位置, 未找到返回-1int lastIndexOf(int ch, int fromIndex)//从fromIndex位置开始前后找第一个字符ch出现的位置, 未找到返回-1//子串str查找int indexOf(String str)//从当前字符串开头向后查找子串str第一次出现的位置, 未找到返回-1int indexOf(String str, int fromIndex)//从当前字符串的fromIndex位置向后查找子串str第一次出现的位置, 未找到返回-1int lastIndexOf(String str)//从当前字符串结尾向前查找子串str第一次出现的位置, 未找到返回-1int lastIndexOf(String str, int fromIndex)//从当前字符串的fromIndex位置向前查找子串str第一次出现的位置, 未找到返回-1char charAt(int index)//返回当前字符串index位置处的字符

Replacement method

//替换String replace(char oldchar, char newchar)//将字符串中所有oldcha字符r替换为newchar字符String replaceFirst(String regex, String replacement)//将字符串中第一个与正则表达式regex匹配的子串用新串replacement替换String replaceAll(String regex, String replacement)//将字符串中所有与正则表达式regex匹配的子串用新串replacement替换String substring(int start[, int end])//返回start到end-1返回的子串, 若省略end, 则为start到串尾.String[] split(String regex)//返回当前字符串通过正则表达式分割的字符串数组

Other methods

Convert digitized strings to basic types

public static  byte  parseByte(String  s) throws NumberFormatExceptionpublic static  short  parseShort(String  s) throws NumberFormatExceptionpublic static  short  parseInt(String  s) throws NumberFormatExceptionpublic static  long  parseLong(String  s) throws NumberFormatExceptionpublic static  float  parseFloat(String  s) throws NumberFormatExceptionpublic static  double  parseDouble(String  s) throws NumberFormatException

Usage examples:

int a = Integer.parseInt(“23”);

Convert other types to string

public static String valueOf(int n)public static String valueOf(char[] data)public static String valueOf(Object obj)public static String copyValueOf(char[] data)等同于valueOf(char[] data)

Usage example:

String.valueOf(334);

StringBuffer class

StringBuffer()//创建空StringBuffer对象StringBuffer(int length)//创建一个长度为length的StringBuffer对象StringBuffer(String str)//创建一个str字符串StringBuffer对象StringBuffer append(Object obj)//将对象obj添加到StringBuffer对象中StringBuffer insert(int position, Object obj)//将对象obj插入到StringBuffer对象中的position位置StringBuffer setCharAt(int position, char ch)//用字符ch替换StringBuffer对象中的position位置StringBuffer deleteCharAt(int position)//删除position位置的字符StringBuffer replace(int start, int end, String str)//将StringBuffer对象中start到end-1的位置用字符串str替换

The above is the detailed content of Commonly used string methods in development. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools