当前位置:网站首页>String common methods
String common methods
2022-08-10 05:50:00 【hagong9】

package com.zhang.String.Demo01;import java.util.Arrays;public class Demo02 {public static void main(String[] args) {String str = "java is the best language in the world, javan is awesome";//length(); returns the length of the stringSystem.out.println(str.length());//charAt(int index) returns the character at a certain positionSystem.out.println(str.charAt(str.length()-2));//Get the last one (counting from the back to the front, the first - a few)//contains (String str); determine if a string is containedSystem.out.println(str.contains("edfsd"));System.out.println(str.contains("world"));System.out.println("==============================================================");//toCharArray() returns the array corresponding to the stringSystem.out.println(Arrays.toString(str.toCharArray()));//indexOf() returns the position of the first occurrence of the stringSystem.out.println(str.indexOf("java"));System.out.println(str.indexOf("java",4));//Look back from the fifth corner mark//lastIndexOf() returns the position of the last occurrence of the stringSystem.out.println(str.lastIndexOf("java"));System.out.println("======================================================");String str2 = "Hello World";//trim() removes the spaces before and after the stringSystem.out.println(str2.trim());//toUpperCase() convert lowercase to uppercase toLowerCase convert uppercase to lowercaseSystem.out.println(str2.toUpperCase());System.out.println(str2.toLowerCase());//endWith(str) determines whether it ends with XX starWith (str) determines whether it starts with xxString str3 = "hello java";System.out.println(str3.endsWith("java"));System.out.println(str3.startsWith("hello"));System.out.println("================================================");//replace(char old, char new) replace the old character with the new characterSystem.out.println(str.replace("java","php"));//split() classifies stringsString say = "java is the best programming language,java";String[] arr = say.split("[ ,]+");//Use space sum, split + means space sum, there can be multiplefor (String string : arr){System.out.println(string);}//equalsString s1 = "hello";String s2 = "HELLO";System.out.println(s1.equalsIgnoreCase(s2));//Ignore case}}
Variable String
StringBuffer; variable-length string, slow and thread-safe.
StingBuilder; variable-length strings, fast, thread-unsafe
package com.zhang.baozhuang.Demo01;public class Demo03 {public static void main(String[] args) {StringBuffer sb = new StringBuffer();//append(); appendsb.append("java world first");System.out.println(sb.toString());sb.append("java is really fragrant");System.out.println(sb.toString());sb.append("java awesome");System.out.println(sb.toString());//insert() addsb.insert(0,"I am at the front");System.out.println(sb.toString());// replace() replacesb.replace(0,4,"hello");//Including the head but not the tail//de;ete() deletesb.delete(0,5);System.out.println(sb.toString());// clearsb.delete(0,sb.length());System.out.println(sb.toString());}}StringBuider is used in the same way as above.
边栏推荐
猜你喜欢

kaggle小白必看:小白常见的2个错误解决方案

深度学习中的学习率调整策略(1)

非会员更改有道云笔记背景

Chain Reading Good Article: Jeff Garzik Launches Web3 Production Company

最新最全的数字藏品发售日历-07.27

21天挑战杯MySQL——Day06

Set Sources Resources and other folders in the IDEA project

链读|最新最全的数字藏品发售日历-07.29

Chain Reading|The latest and most complete digital collection sales calendar-08.02

PCL,VS配置过程中出现:用 _sopen_s 代替 _open, 或用_CRT_SECURE_NO_WARNNINGS错误
随机推荐
Collection工具类
error in ./node_modules/cesium/Source/ThirdParty/zip.js
redis常见的面试题
网络安全3
Knowledge Distillation Thesis Learning
常用类 BigDecimal
Models corresponding to each architecture instruction set
cesium 监听地图缩放或放大来控制地图上添加的内容是否展示
Chain Reading|The latest and most complete digital collection sales calendar-08.02
impdp import data
深度学习中数据到底要不要归一化?实测数据来说明!
ResNet的基础:残差块的原理
Linux database Oracle client installation, used for shell scripts to connect to the database with sqlplus
文章复现:超分辨率网络FSRCNN
shell脚本中利用sqlplus操作数据库
关于cfar检测的学习及仿真
Content related to ZigBee network devices
共识计算和激励机制
网络安全6
链读推荐:从瓷砖到生成式 NFT