基础变量知识:
回顾:上一篇(Salesforce基本变量知识(一))已经写了常用的基本变量Integer,Decimal,Double,Long,ID;那么(Salesforce基本变量知识(二))将写剩下的基本变量String,Boolean。
基本变量:
6、String
String类型和Java中的String类型很类似,在这里不做过多解释,代码中主要需要看一下String类型对象和上述变量如何相互转换,这在项目中是经常用到的,也是必须需要知道的。以下为String类型主要方法:
String goodsName = 'abcd123汉字显示';//测试文本
/*
public String abbreviate(Integer maxWidth)
//译:返回简化字符串,maxWidth>自身长度?自身:maxWidth长度的字符串加上省略号,省略号占3个字符
//注意:maxWidth如果小于4,则抛出Runtime Exception
*/
System.debug('简化后的字符串名称为: '+goodsName.abbreviate(5));//结果:ab...
/*
public String abbreviate(Integer maxWidth,Integer offset)
//译:返回简化字符串,maxWidth为需要简化长度,offset为字符串简化起点
//如果max太小,则抛出Runtime Exception
*/
//返回简化字符串,参数1:最大长度;参数2:偏移量offset
System.debug('简化并添加偏移量的字符串名称为:'+goodsName.abbreviate(5,2));
/*
public String capitalize()
//译:返回当前字符串,其中第一个字母改为标题(大写)。
*/
System.debug('将首字母大写显示'+goodsName.capitalize());
/*
public String center(Integer size)
//译:返回指定大小字符串使原字符串位于中间位置(空格填充左右),如果size<字符串长度,则不起作用
*/
System.debug('设置指定字符串长度为20的显示为:' + goodsName.center(20));
/*
public String center(Integer size,String paddingString)
//译:返回指定大小字符串使原字符串处于中间位置。参数1:字符串显示长度;参数2:填充的字符样式
*/
//返回值:----abcd123汉字显示-----
System.debug('使用-填充字符串显示为:'+goodsName.center(20,'-'));
/*
public Integer charAt(Integer index)
//译:返回对应值得ASC码值
*/
System.debug('goodsName.charAt(5)' + goodsName.charAt(5));
/*
public Integer codePointAt(Integer index)
//译:返回指定位置的值对应的Unicode编码
*/
System.debug('goodsName.codePoint(5)' + goodsName.codePointAt(5));
/*
public Integer codePointBefore(Integer index)
//译:返回指定位置的值前一个对应的Unicode编码
*/
System.debug('goodsName.codePointBefore(5)' + goodsName.codePointBefore(5));
/*
public Integer codePointCount(Integer beginIndex,Integer endIndex)
//译:返回起始位置到截至位置字符串的Unicode编码值
*/
System.debug('goodsName.codePointCount(5,7)' + goodsName.codePointCount(5,7));
/*
public Integer compareTo(String secondString)
//译:基于Unicode比较两个字符串大小,如果小于比较值返回负整数,大于返回正整数,等于返回0
*/
System.debug('两个字符串比较的情况为 : ' + goodsName.compareTo('compareString'));
/*
public Boolean contains(String substring)
//译:判断是否包含某个字符串,包含返回true,不包含返回false
*/
System.debug('商品名称是否包含abcd : ' + goodsName.contains('abcd'));
/*
public Boolean containsAny(String inputString)
//译:判断是否包含inputString任意一个字符,包含返回true,不包含返回false
*/
System.debug('商品名称是否包含abcd任意一个字符:' + goodsName.containsAny('abcd'));
/*
public Boolean containsIgnoreCase(String inputString)
//译:判断是否包含inputString(不区分大小写),包含返回true,不包含返回false
*/
System.debug('商品名称是否包含AbCd(不区分大小写:)' + goodsName.containsIgnoreCase('AbCd'));
/*
public Boolean containsNone(String inputString)
//译:判断是否不包含inputString,不包含返回true,包含返回false
*/
System.debug('商品名称是否不包含abcd'+goodsName.containsNone('abcd'));
/*
public Boolean containsOnly(String inputString)
//译:当前字符串从指定序列只包括inputString返回true,否则返回false
*/
System.debug('商品名称是否只包含abcd:'+ goodsName.containsOnly('abcd'));
/*
public Boolean containsWhitespace()
//译:判断字符串是否包含空格,包含返回true,不包含返回false
*/
System.debug('商品名称是否包含空格 : ' + goodsName.containsWhitespace());
/*
public Integer countMatches(String substring)
//译:判断子字符串在字符串中出现的次数
*/
System.debug('商品名称出现abcd的次数:' + goodsName.countMatches('abcd'));
/*
public String deleteWhitespace()
//译:移除字符串中所有的空格
*/
String removeWhitespaceString = ' a b c d ';
System.debug('原a b c d ,移除空格的字符串显示为:' + removeWhitespaceString.deleteWhitespace());
/*
public String difference(String anotherString)
//译:返回两个字符串之间不同,如果anotherString为空字符串,则返回空字符串,如果anotherString为null,则抛异常
//比较结果以anotherString为基准,从第一个字符比较,不相同则返回anotherString与源字符串不同之处
*/
//返回值:bcd啦啦啦
System.debug('商品名称和abcd啦啦啦的不同返回值为:' + goodsName.difference('bcd啦啦啦'));
/*
public Boolean endsWith(String substring)
//译:判断字符串是否已substring截止,如果是返回true,否则返回false
*/
System.debug('商品名称是否已显示截止:'+goodsName.endsWith('显示'));
/*
public Boolean endsWithIgnoreCase(String substring)
//译:判断字符串是否已substring截止(不区分大小写),如果是返回true,否则返回false
*/
System.debug('商品名称是否已显示截止(不区分大小写) :' + goodsName.endsWithIgnoreCase('显示'));
/*
public Boolean equals(Object anotherString)
//译:判断字符串是否和其他字符串相同
public Boolean equalsIgnoreCase(String anotherString)
//译:判断字符串是否和其他字符串相同(不区分大小写)
*/
String testEquals = 'AbCd123汉字显示';
System.debug('商品名称是否和testEquals字符串相同:' + goodsName.equals(testEquals));
System.debug('商品名称是否和testEquals字符串相同:(不区分大小写)'+goodsName.equalsIgnoreCase(testEquals));
/*
public static String format(String stringToFormat, List<String> formattingArguments)
//译:将转换的参数替换成相同方式的字符串中
*/
String sourceString = 'Hello {0} ,{1} is good';
List<String> formattingArguments = new String[]{'Zero','Apex'};
System.debug('替换sourceString内容以后显示为:'+ String.format(sourceString,formattingArguments));
/*
public static String fromCharArray(List<Integer> charArray)
//译:将char类型数组转换成String类型
*/
List<Integer> charArray = new Integer[] {55,57};
String destinatioin = String.fromCharArray(charArray);
System.debug('通过fromCharArray方法转换的字符串为:' + destinatioin);
/*
public List<Integer> getChars()
//译:返回字符串的字符列表
*/
List<Integer> goodsNameChars = goodsName.getChars();
/*
public static String getCommonPrefix(List<String> strings)
//译:获取列表共有前缀
*/
List<String> strings = new String[]{'abcdf','abe'};
String commonString = String.getCommonPrefix(strings);
System.debug('共有前缀:' + commonString);
/*
public Integer hashCode()
//译:返回字符串的哈希值
*/
Integer hashCode = goodsName.hashCode();
System.debug('商品名称的哈希值为:'+hashCode);
/*
public Integer indexOf(String substring)
//译:返回substring第一次在字符串中出现的位置,如果不存在返回-1
*/
System.debug('cd 在商品名称中出现的位置:' + goodsName.indexOf('cd'));
/*
public Integer indexOf(String substring,Integer index)
//译:返回substring第一次在字符串中出现的位置,起始查询字符串的位置为index处
*/
System.debug('cd 在商品名称中出现的位置:' + goodsName.indexOf('cd',2));
/*
public Integer indexOfAny(String substring)
//译:substring任意一个字符第一次在字符串中出现的位置
*/
System.debug('商品信息中select任意字符最先出现位置:' + goodsName.indexOfAny('select'));
/*
public Integer indexOfAnyBut(String substring)
//译:返回substring任意一个字符不被包含的第一个位置,无则返回-1
*/
System.debug('商品信息中select任意一个字符最先不被包含的位置'+goodsName.indexOfAnyBut('select'));
/*
public Integer indexOfChar(int char)
//译:返回字符串中char字符最先出现的位置
*/
Integer firstChar = goodsName.indexOfChar(55);
/*
public Integer indexOfDifference(String compareTo)
//译:返回两个字符串第一个不同位置的坐标
*/
System.debug('商品名称与abce字符串第一个不同的位置为:' + goodsName.indexOfDifference('abce'));
/*
public Integer indexOfIgnoreCase(String substring)
//译:返回substring在字符串中第一个出现的位置(不考虑大小写)
*/
System.debug('商品名称中第一个出现CD位置的为(不分大小写)' + goodsName.indexOfIgnoreCase('CD'));
/*
public Boolean isAllLowerCase()
//译:字符串是否均为小写,如果是返回true,否则返回false
*/
System.debug('商品名称中是否全是小写: ' + goodsName.isAllLowerCase());
/*
public Boolean isAllUpperCase()
//译:字符串是否均为大写,如果是返回true,否则返回false
*/
System.debug('商品名称中是否全是大写:' + goodsName.isAllUpperCase());
/*
public Boolean isAlpha()
//译:如果当前所有字符均为Unicode编码,则返回true,否则返回false
*/
System.debug('商品名称是否均为Unicode编码:' + goodsName.isAlpha());
/*
public Boolean isAlphanumeric()
//译:如果当前所有字符均为Unicode编码或者Number类型编码,则返回true,否则返回false
*/
System.debug('商品名称是否均为Unicode或者Number类型编码:' + goodsName.isAlphanumeric());
/*
public Boolean isAlphanumericSpace()
//译:如果当前所有字符均为Unicode编码或者Number类型或者空格,则返回true,否则返回false
*/
System.debug('商品名称是否均为Unicode,Number或者空格'+ goodsName.isAlphanumericSpace());
/*
public Boolean isAlphaSpace()
//译:如果当前所有字符均为Unicode或者空格,则返回true,否则返回false
*/
System.debug('商品名称是否均为Unicode,或者空格' +goodsName.isAlphaSpace());
/*
public Boolean isAsciiPrintable()
//译:如果当前所有字符均为可打印的Asc码,则返回true,否则返回false
*/
System.debug('商品名称所有字符是否均为可打印的Asc码:' + goodsName.isAsciiPrintable());
/*
public Boolean isNumeric()
//译:如果当前字符串只包含Unicode的位数,则返回true,否则返回false
*/
System.debug('商品名称所有字符是否均为Unicode的位数:' + goodsName.isNumeric());
/*
public Boolean isWhitespace()
//译:如果当前字符只包括空字符或者空,则返回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,'/');//s = 1/2/3
/*
public Boolean lastIndexOf(String substring)
//译:substring在字符串中最后出现的位置,不存在则返回-1
*/
System.debug('cd最后一次出现的位置:' + goodsName.lastIndexOf('cd'));
/*
public Boolean lastIndexOfIgnoreCase(String substring)
//译:substring在字符串中最后出现的位置(忽略大小写),不存在则返回-1
*/
System.debug('Cd最后一次出现的位置(不考虑大小写):' + goodsName.lastIndexOfIgnoreCase('Cd'));
/*
public String left(Integer index)
//译:获取从零开始到index处的字符串
*/
System.debug('商品名称前三个字符 : abc' + goodsName.left(3));
/*
public String leftPad(Integer length)
//译:返回当前字符串填充的空间左边和指定的长度
*/
String s1 = 'ab';
String s2 = s1.leftPad(4);//s2 = ' ab';
/*
public Integer length()
//译:返回字符串长度
*/
System.debug('商品名称长度:' + goodsName.length());
/*
public String mid(Integer startIndex,Integer length);
//译:返回新的字符串,第一个参数为起始位,第二个字符为字符的长度//类似于substring
*/
System.debug('商品名称截取字符串' + goodsName.mid(2,3));
/*
public String normalizeSpace()
//译:前后删除空白字符
*/
String testNormalizeSpace = 'abc\t\n de';
System.debug('通过normalizeSpace处理后的字符串为:' + testNormalizeSpace.normalizeSpace());
/*
public String remove(String substring)
//译:移除所有特定的子字符串,并返回新字符串
public String removeIgnorecase(String substring)
//译:移除所有特定的子字符串(忽略大小写),并返回新字符串
*/
System.debug('商品名称移除12后的字符串为:' + goodsName.remove('12'));
/*
public String removeEnd(String substring)
//译:当且仅当子字符串在后面移除子字符串
*/
System.debug('当显示在商品名称最后时移除:' + goodsName.removeEnd('显示'));
/*
public String removeStatrt(String substring)
//译:当且仅当子字符串在后面移除子字符串
*/
System.debug('当ab在商品名称最前面时移除' + goodsName.removeStart('ab'));
/*
public String repeat(Integer numberOfTimes)
//译:重复字符串numberOfTimes次数
*/
System.debug('重复商品名称两次的显示为:' + goodsName.repeat(2));
/*
public String repeat(String separator, Integer numberOfTimes)
//译:重复字符串numberOfTimes次,通过separator作为分隔符
*/
System.debug('通过separator分隔符重复字符串两次:' + goodsName.repeat('-',2));
/*
public String replace(String target, String replacement)
//译:将字符串中的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'));
/*
public String reverse()
//译:字符串倒序排列
*/
System.debug('商品名称倒序:' + goodsName.reverse());
/*
public String right(Integer length)
//译:从右查询返回length的个数的字符串
*/
System.debug('返回商品名称后三位字符:' + goodsName.right(3));
/*
public String[] split(String regExp)
//译:通过regExp作为分隔符将字符串分割成数组
*/
String[] sonSplitString = goodsName.split('1');//通过1分割字符串
/*
public String[] split(String regExp, Integer limit)
//译:通过regExp作为分隔符将字符串分割成数组,limit显示数组个数
*/
String [] sonSplitString1 = goodsName.split('1',2);
/*
public Boolean startsWith(string substring)
//译:判断字符串是否以substring开头,如果是返回true,不是返回false
*/
System.debug('商品名称是否以abcd开始:' + goodsName.startsWith('abcd'));
/*
public String substring(Integer length)
//译:截取字符串固定长度
*/
System.debug('截取商品名称前四个字符: ' + goodsName.substring(4));
/*
public String toLowerCase()
//译:将字符串转换成小写
*/
System.debug('商品名称转换成小写:' + goodsName.toLowerCase());
/*
public String toUpperCase()
//译:将字符串转换成大写
*/
System.debug('商品名称转换成大写:' + goodsName.toUpperCase());
/*
public String trim()
//译:去字符串左右空格
*/
System.debug('去空格后商品名称:' + goodsName.trim());
/*
public String uncapitalize()
//译:将字符串第一个转换成小写
*/
System.debug('商品名称第一个单词转换成小写:' + goodsName.uncapitalize());
/*
public static String valueOf(Object objectToConvert)
//译:将Object类型转换成String类型,其中Object类型包括以下:
// Date,DateTime,Decimal,Double,Integer,Long,Object
*/
System.debug('将Double类型转换成String类型' + String.valueOf(3.45));
7、Boolean
Boolean类型声明一个布尔类型,和java区别为:Boolean类型变量有三个取值:true,false,null(default),所以使用Boolean类型声明的时候必须赋予初始值,否则初始值为null。
已完结!