String的方法使用
编程技术  /  houtizong 发布于 3年前   78
split(String regex);split(String regex, int limit);
String szToSplit = ",0,1,2,3,4,5,6,7,8,9,,";String[] arrSplited0 = szToSplit.split(",");String[] arrSplitedA = szToSplit.split(",", 4);String[] arrSplitedB = szToSplit.split(",", -1);String[] arrSplitedC = szToSplit.split(",", 0);
String heaven1 = "paradise";String heaven2 = "paradise";String heaven3 = new String("paradise");System.out.println(heaven1 == heaven2); // trueSystem.out.println(heaven1 == heaven3); // false
String heaven4 = "discriminator" + "paradise";String heaven5 = "discriminator" + "paradise";System.out.println(heaven4 == heaven5); // true
public class StringParadise {private String key;private String value;// Getters and setters are omittedpublic StringParadise(String key1, String key2, String value) {this.key = key1 + key2;this.value = value;}public static void main(String[] args) {StringParadise test1 = new StringParadise("a", "", "paradise");StringParadise test2 = new StringParadise("a", "", "heaven");System.out.println(test1.getKey() == test2.getKey()); // false}}
String strCanonicalA = "Hello, Inteference"; String strCanonicalB = new String("Hello, Inteference");System.out.println(strCanonicalA == strCanonicalB); // falseString strInternB = strCanonicalB.intern();System.out.println(strCanonicalA == strInternB);
public String concat(String str) {int otherLen = str.length();if (otherLen == 0) {return this;}char buf[] = new char[count + otherLen]; // 实例化字符数组getChars(0, count, buf, 0); // 把当前字符串的字符序列放到数组上str.getChars(0, otherLen, buf, count); // 把参数字符串的字符序列附加到数组上return new String(0, count + otherLen, buf); // 构建新的字符串}
Object obj = null;if (obj != null) {String str = obj.toString();}
Object obj = null;String str = (String)obj;
public boolean equals(Object anObject) {if (this == anObject) {return true;}if (anObject instanceof String) {String anotherString = (String)anObject;int n = count;if (n == anotherString.count) {char v1[] = value;char v2[] = anotherString.value;int i = offset;int j = anotherString.offset;while (n-- != 0) {if (v1[i++] != v2[j++])return false;}return true;}}return false;}
public boolean equalsIgnoreCase(String anotherString) {return (this == anotherString) ? true : (anotherString != null) && (anotherString.count == count) && regionMatches(true, 0, anotherString, 0, count);}
// regex为正则表达式,替换第一个匹配的字符串 // replacement中含有'\'或'$',会当成转义或特殊含义处理而被忽略。 public String replaceFirst(String regex, String replacement) { return Pattern.compile(regex).matcher(this).replaceFirst(replacement); } // 同上,替换所有匹配的字符串 public String replaceAll(String regex, String replacement) { return Pattern.compile(regex).matcher(this).replaceAll(replacement); } // 替换所有匹配的字符序列 // Pattern.LITERAL指定target只是普通字符串,不是正则表达式。 // 因为调用了Matcher的quoteReplacement方法,所以'\'或'$'会当作普通字符处理。 public String replace(CharSequence target, CharSequence replacement) { return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(this).replaceAll(Matcher.quoteReplacement(replacement.toString())); } // 替换所有匹配的字符 public String replace(char oldChar, char newChar) { if (oldChar != newChar) { int len = count; int i = -1; char[] val = value; /* avoid getfield opcode */ int off = offset; /* avoid getfield opcode */ while (++i < len) { if (val[off + i] == oldChar) { // 寻找第1个匹配 break; } } if (i < len) { char buf[] = new char[len]; // 创建字符数组 for (int j = 0 ; j < i ; j++) { buf[j] = val[off+j]; // 拷贝第1个匹配之前的字符 } while (i < len) { // 拷贝第1个匹配(包括)后面的字符 char c = val[off + i]; buf[i] = (c == oldChar) ? newChar : c; // 匹配就替换,反之拷贝 i++; } return new String(0, len, buf); // 转化为字符串 } } return this; }
String str = "玉立gorgeous_%520";//str = str.replace('r', '*'); // 玉立go*geous_%520//str = str.replaceFirst("\\w", "*"); // 玉立*orgeous_%520//str = str.replaceAll("\\w", "*"); // 玉立*********%***//str = str.replaceAll("[\\w\u4e00-\u9fa5]", "*"); // ***********%***//str = str.replaceAll("\\w", "$"); // StringIndexOutOfBoundsExceptionstr = str.replace("\r|\n", ""); // 去除回车换行符
public boolean matches(String regex) { return Pattern.matches(regex, this); }
public static boolean matches(String regex, CharSequence input) { Pattern p = Pattern.compile(regex); Matcher m = p.matcher(input); return m.matches(); }
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接