Example Analysis of Java string Operation
This article mainly introduces the Java string operation example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
✨ characters, bytes and strings? Characters and strings
The string contains an array of characters that String can convert to and from char [].
NO method name type description 1public String (char value []) Construction changes all the contents of a character array into a string 2public String (char value [], int offset,int count) Construction changes the contents of a partial character array into a string 3public char charAt (int index) A string that normally gets the specified index position, starting from 0, 4public char [] toChararray () generally changes a string into a character array and returns it.
Code example: gets the characters at the specified location
Public static void main (String [] args) {String str = "hello"; System.out.println (str.charAt (0)); / / subscript starts at 0 System.out.println (str.charAt (1)); System.out.println (str.charAt (2)); System.out.println (str.charAt (3));}
Code example: conversion of string to character array
Public static void main (String [] args) {String str = "helloworld"; / / change a string into a character array char [] data = str.toCharArray (); for (int I = 0; I
< data.length; i++) { System.out.print(data[i]+" "); } } public static void main(String[] args) { String str = "helloworld" ; // 将字符串变为字符数组 char[] data = str.toCharArray() ; // 字符数组转为字符串 System.out.println(new String(data)); // 全部转换 System.out.println(new String(data,5,5)); // 部分转换 } 代码示例: 给定字符串一个字符串, 判断其是否全部由数字所组成 public static boolean isNumberChar(String s) { for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); //判断某个字符是不是数字 if(c < '0' || c >'9') {return false;}} return true;} public static void main (String [] args) {String str = "124567"; System.out.println (isNumberChar (str));}
Public static void main (String [] args) {String str = "1d4567"; System.out.println (isNumberChar (str));}
? Bytes and strings
Bytes are often used in data transmission and transcoding, and String can easily convert to and from byte [].
NO method name type description 1public String (byte bytes []) construction turns a byte array into a string 2public String (byte bytes [], int offset,int length) constructs change the contents of a partial byte array into a string 3public bye [] getBytes () normally returns a string as a byte array 4
Public byte [] getBytes (String charsetNAme) throws
UnsupportedEncodingException
General coding conversion processing
Code example: implement conversion between string and byte array
Public static void main (String [] args) {String str = "helloworld"; / / String to byte [] byte [] data = str.getBytes (); for (int I = 0; I
< data.length; i++) { System.out.print(data[i]+" "); } System.out.println(); // byte[] 转 String System.out.println(new String(data)); } public static void main(String[] args) { byte[] bytes = {97,98,99,100}; String str = new String(bytes,1,3); System.out.println(str); } ????小结 byte[] 是把 String 按照一个字节一个字节的方式处理, 这种适合在网络传输, 数据存储这样的场景下使用. 更适合 针对二进制数据来操作. char[] 是吧 String 按照一个字符一个字符的方式处理, 更适合针对文本数据来操作, 尤其是包含中文的时候. ✨字符串常见操作????字符串比较No方法名称类型描述1public boolean equals(Object anObject)普通区分大小的比较2public boolean equalsIanorecase(String anotherString)普通不区分大小写的比较3public int compareTo(String anotherString)普通比较两个字符串大小关系 代码示例: 不区分大小写比较 public static void main(String[] args) { String str1 = "hello" ; String str2 = "Hello" ; System.out.println(str1.equals(str2)); // false System.out.println(str1.equalsIgnoreCase(str2)); // true } 在String类中compareTo()方法是一个非常重要的方法,该方法返回一个整型,该数据会根据大小关系返回三类内容: 1. 相等:返回0. 2. 小于:返回内容小于0. 3. 大于:返回内容大于0。 public static void main(String[] args) { System.out.println("A".compareTo("a")); // -32 System.out.println("a".compareTo("A")); // 32 System.out.println("A".compareTo("A")); // 0 System.out.println("AB".compareTo("AC")); // -1 System.out.println("刘".compareTo("杨")); } compareTo()是一个可以区分大小关系的方法,是String方法里是一个非常重要的方法。 字符串的比较大小规则, 总结成三个字 "字典序" 相当于判定两个字符串在一本词典的前面还是后面. 先比较第一 个字符的大小(根据 unicode 的值来判定), 如果不分胜负, 就依次比较后面的内容 ????字符串查找 从一个完整的字符串之中可以判断指定内容是否存在,对于查找方法有如下定义: NO方法名称类型描述1public boolean contains(CharSequence s)普通判断一个子字符串是否存在2public int indexOf(String str)普通从头开始查找指定字符串的位置,查到了返回位置的开始索引,如果查不到返回-13public int indexOf(String str,int fromIndex)普通从指定位置查找子字符串位置4public int LastIndexOf(String str)普通从后向前查找子字符串位置5public int LastIndexOf(String str, int fromIdex)普通从指定位置由后向前查找6public boolean startWith (String prefix)普通判断是否以指定字符串开头7public boolean startWith(String prefix, int toffset)普通从指定位置开始判断是否以指定字符串开头8public boolean endWith(String suffix)普通判断是否以指定字符串结尾 代码示例: 字符串查找,最好用最方便的就是contains() public static void main(String[] args) { String str = "helloworld" ; System.out.println(str.contains("world")); System.out.println(str.contains("forld")); } 代码示例: 使用indexOf()方法进行位置查找 public static void main(String[] args) { String str = "helloworld" ; System.out.println(str.indexOf("world")); // 5,w开始的索引 System.out.println(str.indexOf("bit")); // -1,没有查到 if (str.indexOf("hello") != -1) { System.out.println("可以查到指定字符串!"); } } 代码示例: 使用indexOf()的注意点 public static void main(String[] args) { String str = "helloworld" ; System.out.println(str.indexOf("l")); // 2 System.out.println(str.indexOf("l",5)); // 8 System.out.println(str.lastIndexOf("l")); // 8 } 代码示例: 判断开头或结尾 public static void main(String[] args) { String str = "**@@helloworld!!" ; System.out.println(str.startsWith("**")); // true System.out.println(str.startsWith("@@",2)); // ture System.out.println(str.endsWith("!!")); // true } ????字符串替换 使用一个指定的新的字符串替换掉已有的字符串数据,可用的方法如下 No方法名称类型描述1public String replaceAll(String regex,String replacement)普通替换所有指定的内容2public String replaceFirst(String regex, String replacement)普通替换首个内容 代码示例: 字符串的替换处理 public static void main(String[] args) { String str = "helloworld" ; System.out.println(str.replaceAll("l", "_")); System.out.println(str.replaceFirst("l", "_")); } 注意事项: 由于字符串是不可变对象 , 替换不修改当前字符串, 而是产生一个新的字符串 ????字符串拆分 可以将一个完整的字符串按照指定的分隔符划分为若干个子字符串。 NO方法名称类型描述1public String[] split(String regex)普通将字符串全部拆分2public String[] split(String regex,int limit)普通将字符串部分拆分 代码示例: 实现字符串的拆分处理 public static void main(String[] args) { String str = "hello world hello yu" ; String[] result = str.split(" ") ; // 按照空格拆分 for(String s: result) { System.out.println(s); } } 代码示例: 字符串的部分拆分 public static void main(String[] args) { String str = "hello world hello yu" ; String[] result = str.split(" ",2) ; for(String s: result) { System.out.println(s); } } 代码示例: 拆分IP地址 public static void main(String[] args) { String str = "192.168.1.1" ; String[] result = str.split("\\.") ; for(String s: result) { System.out.println(s); } } 注意事项: 1. 字符"|","*","+"都得加上转义字符,前面加上"\". 2. 而如果是"",那么就得写成"\\". 3. 如果一个字符串中有多个分隔符,可以用"|"作为连字符 代码示例: 多次拆分 public static void main(String[] args) { String str = "name=zhangsan&age=18" ; String[] result = str.split("&") ; for (int i = 0; i < result.length; i++) { String[] temp = result[i].split("=") ; System.out.println(temp[0]+" = "+temp[1]); } } ????字符串截取 从一个完整的字符串之中截取出部分内容。可用方法如下: NO方法名称类型描述1public String substring(int beginIndex)普通从指定索引截取到结尾2public String substring(int beginIndex, int endIndex)普通截取部分内容 代码示例: 观察字符串截取 public static void main(String[] args) { String str = "helloworld" ; System.out.println(str.substring(5)); System.out.println(str.substring(0, 5)); } 注意事项: 1. 索引从0开始 2. 注意前闭后开区间的写法, substring(0, 5) 表示包含 0 号下标的字符, 不包含 5 号下标 ????其他操作方法NO方法名称类型描述1public String trim()普通去掉字符串的左右空格,保留中间空格2public String toUpperCase()普通字符串转大写3public String toLowerCase()普通字符串转小写4public native String intern()普通字符串入池操作5public String concat(String str)普通字符串连接,等同于+,不入池6public int length()普通取得字符串长度7public boolean isEmpty普通判断是否为空字符串,但不是null,而是长度0 代码示例: 观察trim()方法的使用 public static void main(String[] args) { String str = " hello world " ; System.out.println("["+str+"]"); System.out.println("["+str.trim()+"]"); } 代码示例: 大小写转换 public static void main(String[] args) { String str = " hello%$$%@#$%world 哈哈哈 " ; System.out.println(str.toUpperCase()); System.out.println(str.toLowerCase()); } 代码示例: 字符串length() public static void main(String[] args) { String str = " hello%$$%@#$%world 哈哈哈 " ; System.out.println(str.length()); } 注意:数组长度使用数组名称.length属性,而String中使用的是length()方法 代码示例: 观察isEmpty()方法 public static void main(String[] args) { System.out.println("hello".isEmpty()); System.out.println("".isEmpty()); System.out.println(new String().isEmpty()); } String类并没有提供首字母大写操作,需要自己实现 代码示例: 首字母大写 public static void main(String[] args) { System.out.println(fistUpper("yuisama")); System.out.println(fistUpper("")); System.out.println(fistUpper("a")); } public static String fistUpper(String str) { if ("".equals(str)||str==null) { return str ; } if (str.length()>1) {return str.substring (0,1) .toUpperCase () + str.substring (1);} return str.toUpperCase ();}
Thank you for reading this article carefully. I hope the article "sample Analysis of Java string Operation" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!