55、saleforce 学习笔记二

String goodsName = 'abcd1123汉字显示';//测试文本

System.debug('简化后的字符串名称为:'+goodsName.abbreviate(5));

//返回简化字符串,参数1:最大长度;参数2:偏移量offset
System.debug('简化并添加偏移量的字符串名称为:'+goodsName.abbreviate(5,2));

System.debug('将首字母大写显示'+goodsName.capitalize());

//译:返回指定大小字符串使原字符串位于中间位置(空格填充左右),如果size<字符串长度,则不起作用
System.debug('设置指定字符串长度为20的显示为:'+goodsName.center(20));

//返回值:----abcd123汉字显示---
System.debug('使用-填充字符串显示为:'+goodsName.center(20,'-'));

//译:返回对应值得ASC码值
System.debug('goodsName.charAt(5)'+goodsName.charAt(5));

//译:返回指定位置的值对应的Unicode编码
System.debug('goodsName.codePoint(5)'+goodsName.codePointAt(5));

//译:返回指定位置的值前一个对应的Unicode编码
System.debug('goodsName.codePointBefore(5)'+goodsName.codePointBefore(5));

//译:返回起始位置到截至位置字符串的Unicode编码值
System.debug('goodsName.codePointCount(5,7)'+goodsName.codePointCount(5,7));

//译:基于Unicode比较两个字符串大小,如果小于比较值返回负整数,大于返回正整数,等于返回0
System.debug('两个字符串比较的情况为:'+goodsName.compareTo('compareString'));

//译:判断是否包含某个字符串,包含返回true,不包含返回false
System.debug('商品名称是否包含abcd:'+goodsName.contains('abcd'));

//译:判断是否包含inputString任意一个字符,包含返回true,不包含返回false
System.debug('商品名称是否包含abcd任意一个字符串:'+goodsName.containsAny('abcd'));

//判断是否包含inputString(不区分大小写),包含返回true.不包含返回false
System.debug('商品名称是否包含AbCd(不区分大小写:)'+goodsName.containsIgnoreCase('AbCd'));

//判断是否不包含inputString,不包含返回true,包含返回false
System.debug('商品名称是否不包含abcd'+goodsName.containsNone('abcd'));

//译:当前字符串从指定序列只包括inputString返回true,否则返回false
System.debug('商品名称是否只包含abcd:'+goodsName.containsOnly('abcd'));

//判断字符串是否包含空格,包含返回true,不包含返回false
System.debug('商品名称是否包含空格:'+goodsName.containsWhitespace());

//判断字符串在字符串中出现的此时
System.debug('商品名称出现abcd的次数:'+goodsName.countMatches('abcd'));

//移除字符串中所有的空格
String removeWhitespaceString = 'a b c d ';
System.debug('原  a b c d ,移除空格的字符串显示为:'+removeWhitespaceString.deleteWhitespace());

//返回两个字符串之间不同,如果anotherString为空字符串,则返回空字符串,如果anotherString为null,则抛异常
System.debug('商品名称和abcd啦啦啦的不同返回值为:'+goodsName.difference('bcd啦啦啦'));

//判断字符串是否已substring截止,如果是返回true,否则返回false
System.debug('商品名称是否已 显示  截止:'+goodsName.endsWith('显示'));

//判断字符串是否已substring截止(不区分大小写),如果是返回true,否则返回false
System.debug('商品名称是否已  显示  截止(不区分大小写):'+goodsName.endsWithIgnoreCase('显示'));

String testEquals = 'AbCd123汉字显示';
System.debug('商品名称是否和testEquals字符串相同:'+goodsName.equals(testEquals));

System.debug('商品名称是否和testEquals字符串相同:(不区分大小写)'+goodsName.equalsIgnoreCase(testEquals));


//返回字符串的字符列表
List<Integer> goodsNameChars = goodsName.getChars();

//获取列表共有前缀
List<String> strings = new String[]{'abcdf','abe'};
String commonString = String.getCommonPrefix(strings);
System.debug('共有前缀:'+commonString);

//返回字符串的哈希值
Integer hashCode = goodsName.hashCode();
System.debug('商品名称的哈希值为:'+hashCode);


//返回substring第一次在字符串中出现的位置,如果不存在返回-1
System.debug('cd在商品名称中出现的位置:'+ goodsName.indexOf('cd'));

//substring任意一个字符第一次在字符串中出现的位置
System.debug('商品信息中select任意字符最先出现位置:'+ goodsName.indexOfAny('select'));

//返回substring任意一个字符不被包含的第一个位置,无则返回-1
System.debug('商品信息中select任意一个字符最先不被包含的位置'+goodsName.indexOfAnyBut('select'));

//返回字符串中char字符最先出现的位置
Integer firstChar = goodsName.indexOfChar(55);

//返回两个字符串第一个不同位置的坐标
System.debug('商品名称与abce字符串第一个不同的位置为:'+goodsName.indexOfDifference('abce'));

//返回substring在字符串中第一个出现的位置(不考虑大小写)
System.debug('商品名称中第一个出现CD位置的为(不分大小写)'+goodsName.indexOfIgnoreCase('CD'));

//字符串是否均为小写,如果是返回true,否则返回false
System.debug('商品名称中是否全是小写:'+goodsName.isAllLowerCase());

//字符串是否均为大写,如果是返回true,否则返回false
System.debug('商品名称是否全是大写:'+goodsName.isAllUpperCase());

//如果当前所有字符均为Unicode编码,则返回true,否则返回false
System.debug('商品名称是否均为Unicode编码:'+goodsName.isAlpha());

//如果当前所有字符均为Unicode编码或者Number类型编码,则返回true,否则返回false
System.debug('商品名称是否均为Unicode编码或者Number类型编码:'+goodsName.isAlphanumeric());

//如果当前所有字符均为Unicode编码或者Number类型或者空格,则返回true,否则返回false
System.debug('商品名称是否均为Unicode,Number或者空格' + goodsName.isAlphanumericSpace());

//如果当前所有字符均为Unicode或者空格,则返回true,否则返回false
System.debug('商品名称是否均为Unicode,或者空格'+ goodsName.isAlphaSpace());

//如果当前所有字符均为可打印的Asc码,则返回true,否则返回false
System.debug('商品名称所有字符是否均为可打印的Asc码:'+ goodsName.isAsciiPrintable());

String goodsName = 'abcd1123汉字显示';//测试文本

//如果当前字符串只包含Unicode的位数,则返回true,否则返回false
System.debug('商品名称所有字符是否均为Unicode的位数'+ goodsName.isNumeric());

//如果当前字符只包括空字符或者空,则返回true,否则返回false
System.debug('商品名称所有字符只包括空字符或者空:' + goodsName.isWhitespace());


//public static String join(Object iterableObj, String separator)
//译:通过separator连接对象,通用于数组,列表等
List<Integer> intLists = new Integer[] {1,2,3};
String s = String.join(intLists, '/');

//substring在字符串中最后出现的位置,不存在则返回-1
System.debug('cd最后一次出现的位置:'+goodsName.lastIndexOf('cd'));

//substring在字符串中最后出现的位置(忽略大小写),不存在则返回-1
System.debug('Cd最后一次出现的位置(不考虑大小写):' + goodsName.lastIndexOfIgnoreCase('Cd'));

//获取从零开始到index处的字符串
System.debug('商品名称前三个字符:abc' + goodsName.left(3));

//返回当前字符串填充的空间左边和指定的长度
String s1 = 'ab';
String s2 = s1.leftPad(4);

//译:返回字符串长度
System.debug('商品名称长度:' + goodsName.length());

 //译:返回新的字符串,第一个参数为起始位,第二个字符为字符的长度//类似于substring
System.debug('商品名称截取字符串'+goodsName.mid(2,3));

//前后删除空白字符
String testNormalizeSpace = 'abc	
 de';
System.debug('通过normalizeSpace处理后的字符串为:'+testNormalizeSpace.normalizeSpace());

//译:移除所有特定的子字符串(忽略大小写),并返回新字符串
System.debug('商品名称移除12后的字符串为:' + goodsName.remove('12'));

//译:当且仅当子字符串在后面移除子字符串
System.debug('当显示在商品名称最后时移除:' + goodsName.removeEnd('显示'));

//译:当且仅当子字符串在后面移除子字符串
System.debug('当ab在商品名称最前面时移除' + goodsName.removeStart('ab'));

//译:重复字符串numberOfTimes次数
System.debug('重复商品名称两次的显示为:' + goodsName.repeat(2));

//重复字符串numberOfTimes次,通过separator作为分隔符
System.debug('通过separator分隔符重复字符串两次:' + goodsName.repeat('-',2));

//译:将字符串中的target转换成replacement
System.debug('将商品名称中ab替换成AB' + goodsName.replace('ab','AB'));


// public String replaceAll(String target,String replacement)
System.debug('将商品名称中ab全部替换成AB' + goodsName.replaceAll('ab','AB'));

//字符串倒序排列
System.debug('商品名称倒序:' + goodsName.reverse());


//译:从右查询返回length的个数的字符串
System.debug('返回商品名称后三位字符:' + goodsName.right(3));

//译:通过regExp作为分隔符将字符串分割成数组
String[] sonSplitString = goodsName.split('1');//通过1分割字符串

//译:通过regExp作为分隔符将字符串分割成数组,limit显示数组个数
String [] sonSplitString1 = goodsName.split('1',2);

//译:判断字符串是否以substring开头,如果是返回true,不是返回false
System.debug('商品名称是否以abcd开始:' + goodsName.startsWith('abcd'));

//译:截取字符串固定长度
System.debug('截取商品名称前四个字符: ' + goodsName.substring(4));

//译:将字符串转换成小写
System.debug('商品名称转换成小写:' + goodsName.toLowerCase());


//译:去字符串左右空格
System.debug('去空格后商品名称:' + goodsName.trim());

//译:将字符串第一个转换成小写
System.debug('商品名称第一个单词转换成小写:' + goodsName.uncapitalize());

//译:将Object类型转换成String类型,其中Object类型包括以下:
// Date,DateTime,Decimal,Double,Integer,Long,Object
//译:将字符串转换成大写
System.debug('商品名称转换成大写:' + goodsName.toUpperCase());


//译:从右查询返回length的个数的字符串
System.debug('返回商品名称后三位字符:' + goodsName.right(3));

原文地址:https://www.cnblogs.com/weizhen/p/6386650.html