Maison > Java > javaDidacticiel > le corps du texte

Méthodes de chaînes couramment utilisées en développement

巴扎黑
Libérer: 2017-06-26 11:11:35
original
1208 Les gens l'ont consulté

La fonction de Java String peut être considérée comme très puissante, et chacune de ses méthodes est également très utile.

Il existe deux classes de chaînes couramment utilisées dans les chaînes Java, à savoir la classe String et le StringBuffer. class .

Classe Sting

Les objets de la classe String sont immuables.

Créer une chaîne

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

Méthodes courantes

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

Exemples :

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"
Copier après la connexion

Méthodes de propriétés comparatives

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结尾
Copier après la connexion

Méthode de recherche

//字符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位置处的字符
Copier après la connexion

Méthode de remplacement

//替换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)//返回当前字符串通过正则表达式分割的字符串数组
Copier après la connexion

Autres méthodes

Convertir des chaînes numériques en types de base

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
Copier après la connexion

Exemples d'utilisation :

int a = Integer.parseInt(“23”);
Copier après la connexion

Convertir d'autres types en chaînes

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)
Copier après la connexion

Exemple d'utilisation :

String.valueOf(334);
Copier après la connexion

Classe StringBuffer

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替换
Copier après la connexion

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!