salesforce若是簡單的說能夠大概分紅兩個部分:Apex,VisualForce Page.java
其中Apex語言和java不少的語法相似,今天總結的是一些簡單的Apex的變量等知識。數據庫
有以下幾種經常使用的基本變量Integer,String,Decimal,Double,Long,Boolean,ID。編程
集合經常使用的對象:List<T>,Set<T>,Map<T>。api
時間日期經常使用對象:Datetime,Time,Date。數組
其餘:Object,sObject(與數據庫相關,之後篇會講)ide
與JAVA一個最大的區別是:Apex中基本對象的初始值均爲null。函數
eg:學習
Integer i; i += 1; System.debug(i);
在java中此種寫法是能夠的,由於int類型初始值爲0,i+=1之後則i變成1.可是在Apex中由於i初始值爲null。
因此i+=1在運行時會拋出NullPointerException
固然,比較有意思的事情是這樣,直接上代碼:測試
Integer i; System.debug(i+'1');
此種方法輸出的結果則爲null1。起始這也不奇怪,由於Apex也是基於java拓展的,若是看java編程思想了解底層的null的toString()方法處理也就知道了,當執行Print操做時,一個變量爲null時,他的toString方法則返回'null'字符串。固然,這個只是一個拓展,很少展開,若是感興趣,能夠查看一下java的api或者看一下java編程思想一書。編碼
一)基本變量:
1)Integer
Integer表示一個32位整數的對象,取值範圍爲-2^31 -- 2^31.
Integer主要有兩個方法:
/* public String format() //譯:將Integer值轉換成String類型的值 */ Integer goodsCount = 12; System.debug('將Integer值轉成String: ' + goodsCount.format()); /* public static Integer valueOf(String stringToObject) //譯:將String類型轉成Integer類型 */ Integer goodsCountI = Integer.valueOf('12');
2)Long
Long類型表示一個64位整數的對象,取值範圍爲-2^63--2^63-1.
Integer類型能夠直接轉換成Long類型,Long類型在不超過範圍狀況下能夠經過intValue()方法轉成Integer類型。
如下爲Long類型部分主要方法:
Integer transferSource = 12345;
Long code = transferSource;//Integer類型能夠直接轉成Long類型 /* public String format() //譯:將Long類型轉換成String類型 */ System.debug('Long類型轉成String類型:' + code.format()); /* public Integer intValue() //譯:將Long類型轉成Integer類型 */ System.debug('將Long類型轉成Integer類型:' + code.intValue()); /* public static Long valueOf(String stringToLong) //譯:將String類型轉成Long類型 */ Long codeLong = Long.valueOf('123');
3)ID
ID類型能夠用任何一個符合規則的18位字符表示,若是你設置ID字符爲15位,則將字符自動擴展成18位。不符合規則的ID字符在運行時則運行時異常。
如下爲ID的主要方法:
/* public static ID valueOf(String toID) //譯:將toId轉換成ID */ String idStr = '111111111111111111'; ID id = ID.valueOf(idStr); /* public Boolean equals(String id) //譯:判斷兩個ID是否相同 */ Boolean isEquals = id.equals(idStr);
4)Decimal
簡單的來講,Decimal變量的意思爲包含小數點的32位數就是Decimal,很像java中的float類型變量。
如下爲Decimal的部分主要方法用來了解Decimal的功能:
1 Decimal priceDecimal = -4.50; 2 /* 3 public Decimal abs() 4 //譯:返回小數點的絕對值 5 */ 6 System.debug('小數的絕對值爲:' + priceDecimal.abs()); 7 8 /* 9 public Decimal divide(Decimal divisor, Integer scale) 10 //譯:經過divisor做爲除數,並設置結果爲特定的scale的位數 11 */ 12 System.debug('priceDecimal除以10小數點保留兩位小數: ' + priceDecimal.divide(10,2));//-0.45 13 14 /* 15 public Double doubleValue() 16 //譯:將Decimal類型轉換成Double類型 17 */ 18 System.debug('將priceDecimal轉換成Double類型' + priceDecimal.doubleValue()); 19 20 /* 21 public String format() 22 //譯:將Decimal轉成String類型 23 */ 24 System.debug('Decimal轉成String類型' + priceDecimal.format()); 25 26 /* 27 public Integer intValue() 28 //譯:將Decimal轉成Integer 29 */ 30 System.debug('將Decimal轉成Integer類型' + priceDecimal.intValue()); 31 32 /* 33 public Long longValue() 34 //譯:將Decimal轉成Long類型 35 */ 36 System.debug('將Decimal轉成Long類型' + priceDecimal.longValue()); 37 38 /* 39 public Decimal pow(int exponent) 40 譯:返回Decimal對應的指數次冪值.若是Decimal值爲0,則返回1 41 */ 42 System.debug('priceDecimal平方值爲:' + priceDecimal.pow(2)); 43 44 /* 45 public Integer precision() 46 //譯:返回Decimal值得數字的總數 47 */ 48 System.debug('priceDecimal數字總數爲:' + priceDecimal.precision());//2 -4.5 有4和5 49 50 51 /* 52 public Long round() 53 //譯:將Decimal值轉換成最接近Long類型的值,四捨五入 54 */ 55 System.debug('priceDecimal四捨五入Long類型值爲:' + priceDecimal.round()); 56 57 /* 58 public Integer scale() 59 //譯:返回小數點後的位數個數 60 */ 61 System.debug('priceDecimal小數點後的位數爲:' + priceDecimal.scale()); 62 63 /* 64 public Decimal setScale(Integer scale) 65 //譯:設置小數的小數點後的位數 66 */ 67 System.debug('設置priceDecimal的小數爲2位' + priceDecimal.setScale(2)); 68 69 /* 70 public Decimal stripTrailingZeros() 71 //譯:返回移除0之後的小數 72 */ 73 System.debug('移除priceDecimal小數點後的0之後的值爲:' + priceDecimal.stripTrailingZeros()); 74 75 /* 76 public String toPlainString() 77 //譯:返回Decimal轉換後的String,Decimal值不使用科學記數法 78 */ 79 System.debug('不使用科學記數法轉換成String類型' + priceDecimal.toPlainString()); 80 /* 81 Decimal.valueOf(Object objectToDecimal) 82 //譯:將Object轉成Decimal。其中Object能夠爲Double,Long, String 83 */ 84 Long priceL = 12345; 85 Double priceD = 123.456; 86 String priceS = '12345'; 87 Decimal d1 = Decimal.valueOf(priceL); 88 Decimal d2 = Decimal.valueOf(priceD); 89 Decimal d3 = Decimal.valueOf(priceS);
5)Double
Double變量爲包含小數點的64位數,很像 java中的Double類型變量。
Decimal類型變量能夠直接轉換成Double類型變量,Double類型在不超過範圍狀況下能夠經過
如下爲Double的部分主要方法:
Double price = 34.5678; /* public static Double valueOf(String stringToDouble) //譯:將String類型轉換成Double */ String doubleString = '3.89'; System.debug('將字符串轉換成Double' + Double.valueOf(doubleString)); /* public Long round() //譯:返回double最接近Long的值,四捨五入 */ Long priceLong = price.round(); System.debug('經過round方法將double轉換成Long類型值爲:' + priceLong); /* public Integer intValue() //譯:將double值轉換成int類型值 */ Integer priceInteger = price.intValue(); System.debug('將double轉換成Integer類型值爲:' + priceInteger); Long priceLongByLongValue = price.longValue(); System.debug('將double轉換成Long類型值爲:' + priceLongByLongValue);
6)String
String類型和Java中的String類型很相似,在這裏不作過多解釋,代碼中主要須要看一下String類型對象和上述變量如何相互轉換,這在項目中是常常用到的,也是必須須要知道的。如下爲String類型主要方法:
1 String goodsName = 'abcd123漢字顯示';//測試文本 2 /* 3 public String abbreviate(Integer maxWidth) 4 //譯:返回簡化字符串,maxWidth>自身長度?自身:maxWidth長度的字符串加上省略號,省略號佔3個字符 5 //注意:maxWidth若是小於4,則拋出Runtime Exception 6 */ 7 8 System.debug('簡化後的字符串名稱爲: '+goodsName.abbreviate(5));//結果:ab... 9 10 /* 11 public String abbreviate(Integer maxWidth,Integer offset) 12 //譯:返回簡化字符串,maxWidth爲須要簡化長度,offset爲字符串簡化起點 13 //若是max過小,則拋出Runtime Exception 14 */ 15 //返回簡化字符串,參數1:最大長度;參數2:偏移量offset 16 System.debug('簡化並添加偏移量的字符串名稱爲:'+goodsName.abbreviate(5,2)); 17 18 /* 19 public String capitalize() 20 //譯:返回當前字符串,其中第一個字母改成標題(大寫)。 21 */ 22 23 System.debug('將首字母大寫顯示'+goodsName.capitalize()); 24 25 /* 26 public String center(Integer size) 27 //譯:返回指定大小字符串使原字符串位於中間位置(空格填充左右),若是size<字符串長度,則不起做用 28 */ 29 30 System.debug('設置指定字符串長度爲20的顯示爲:' + goodsName.center(20)); 31 32 /* 33 public String center(Integer size,String paddingString) 34 //譯:返回指定大小字符串使原字符串處於中間位置。參數1:字符串顯示長度;參數2:填充的字符樣式 35 */ 36 //返回值:----abcd123漢字顯示----- 37 System.debug('使用-填充字符串顯示爲:'+goodsName.center(20,'-')); 38 39 /* 40 public Integer charAt(Integer index) 41 //譯:返回對應值得ASC碼值 42 */ 43 System.debug('goodsName.charAt(5)' + goodsName.charAt(5)); 44 45 /* 46 public Integer codePointAt(Integer index) 47 //譯:返回指定位置的值對應的Unicode編碼 48 */ 49 50 System.debug('goodsName.codePoint(5)' + goodsName.codePointAt(5)); 51 52 /* 53 public Integer codePointBefore(Integer index) 54 //譯:返回指定位置的值前一個對應的Unicode編碼 55 */ 56 System.debug('goodsName.codePointBefore(5)' + goodsName.codePointBefore(5)); 57 58 /* 59 public Integer codePointCount(Integer beginIndex,Integer endIndex) 60 //譯:返回起始位置到截至位置字符串的Unicode編碼值 61 */ 62 63 System.debug('goodsName.codePointCount(5,7)' + goodsName.codePointCount(5,7)); 64 65 /* 66 public Integer compareTo(String secondString) 67 //譯:基於Unicode比較兩個字符串大小,若是小於比較值返回負整數,大於返回正整數,等於返回0 68 */ 69 System.debug('兩個字符串比較的狀況爲 : ' + goodsName.compareTo('compareString')); 70 71 /* 72 public Boolean contains(String substring) 73 //譯:判斷是否包含某個字符串,包含返回true,不包含返回false 74 */ 75 76 System.debug('商品名稱是否包含abcd : ' + goodsName.contains('abcd')); 77 78 /* 79 public Boolean containsAny(String inputString) 80 //譯:判斷是否包含inputString任意一個字符,包含返回true,不包含返回false 81 */ 82 System.debug('商品名稱是否包含abcd任意一個字符:' + goodsName.containsAny('abcd')); 83 84 /* 85 public Boolean containsIgnoreCase(String inputString) 86 //譯:判斷是否包含inputString(不區分大小寫),包含返回true,不包含返回false 87 */ 88 89 System.debug('商品名稱是否包含AbCd(不區分大小寫:)' + goodsName.containsIgnoreCase('AbCd')); 90 /* 91 public Boolean containsNone(String inputString) 92 //譯:判斷是否不包含inputString,不包含返回true,包含返回false 93 */ 94 System.debug('商品名稱是否不包含abcd'+goodsName.containsNone('abcd')); 95 96 /* 97 public Boolean containsOnly(String inputString) 98 //譯:當前字符串從指定序列只包括inputString返回true,不然返回false 99 */ 100 System.debug('商品名稱是否只包含abcd:'+ goodsName.containsOnly('abcd')); 101 102 /* 103 public Boolean containsWhitespace() 104 //譯:判斷字符串是否包含空格,包含返回true,不包含返回false 105 */ 106 System.debug('商品名稱是否包含空格 : ' + goodsName.containsWhitespace()); 107 108 /* 109 public Integer countMatches(String substring) 110 //譯:判斷子字符串在字符串中出現的次數 111 */ 112 System.debug('商品名稱出現abcd的次數:' + goodsName.countMatches('abcd')); 113 114 /* 115 public String deleteWhitespace() 116 //譯:移除字符串中全部的空格 117 */ 118 String removeWhitespaceString = ' a b c d '; 119 System.debug('原 a b c d ,移除空格的字符串顯示爲:' + removeWhitespaceString.deleteWhitespace()); 120 121 /* 122 public String difference(String anotherString) 123 //譯:返回兩個字符串之間不一樣,若是anotherString爲空字符串,則返回空字符串,若是anotherString爲null,則拋異常 124 //比較結果以anotherString爲基準,從第一個字符比較,不相同則返回anotherString與源字符串不一樣之處 125 */ 126 //返回值:bcd啦啦啦 127 System.debug('商品名稱和abcd啦啦啦的不一樣返回值爲:' + goodsName.difference('bcd啦啦啦')); 128 129 /* 130 public Boolean endsWith(String substring) 131 //譯:判斷字符串是否已substring截止,若是是返回true,不然返回false 132 */ 133 System.debug('商品名稱是否已 顯示 截止 :' + goodsName.endsWith('顯示')); 134 135 /* 136 public Boolean endsWithIgnoreCase(String substring) 137 //譯:判斷字符串是否已substring截止(不區分大小寫),若是是返回true,不然返回false 138 */ 139 System.debug('商品名稱是否已 顯示 截止(不區分大小寫) :' + goodsName.endsWithIgnoreCase('顯示')); 140 141 /* 142 public Boolean equals(Object anotherString) 143 //譯:判斷字符串是否和其餘字符串相同 144 */ 145 146 /* 147 public Boolean equalsIgnoreCase(String anotherString) 148 //譯:判斷字符串是否和其餘字符串相同(不區分大小寫) 149 */ 150 151 String testEquals = 'AbCd123漢字顯示'; 152 153 System.debug('商品名稱是否和testEquals字符串相同:' + goodsName.equals(testEquals)); 154 155 System.debug('商品名稱是否和testEquals字符串相同:(不區分大小寫)'+goodsName.equalsIgnoreCase(testEquals)); 156 157 /* 158 public static String format(String stringToFormat, List<String> formattingArguments) 159 //譯:將轉換的參數替換成相同方式的字符串中 160 */ 161 String sourceString = 'Hello {0} ,{1} is good'; 162 List<String> formattingArguments = new String[]{'Zero','Apex'}; 163 System.debug('替換sourceString內容之後顯示爲:' + String.format(sourceString,formattingArguments)); 164 165 /* 166 public static String fromCharArray(List<Integer> charArray) 167 //譯:將char類型數組轉換成String類型 168 */ 169 List<Integer> charArray = new Integer[] {55,57}; 170 String destinatioin = String.fromCharArray(charArray); 171 System.debug('經過fromCharArray方法轉換的字符串爲:' + destinatioin); 172 173 174 /* 175 public List<Integer> getChars() 176 //譯:返回字符串的字符列表 177 */ 178 List<Integer> goodsNameChars = goodsName.getChars(); 179 180 /* 181 public static String getCommonPrefix(List<String> strings) 182 //譯:獲取列表共有前綴 183 */ 184 List<String> strings = new String[]{'abcdf','abe'}; 185 String commonString = String.getCommonPrefix(strings); 186 System.debug('共有前綴:' + commonString); 187 188 //goodsName.getLevenshteinDistance();//待學習 189 190 /* 191 public Integer hashCode() 192 //譯:返回字符串的哈希值 193 */ 194 Integer hashCode = goodsName.hashCode(); 195 System.debug('商品名稱的哈希值爲:'+hashCode); 196 /* 197 public Integer indexOf(String substring) 198 //譯:返回substring第一次在字符串中出現的位置,若是不存在返回-1 199 */ 200 System.debug('cd 在商品名稱中出現的位置:' + goodsName.indexOf('cd')); 201 202 /* 203 public Integer indexOf(String substring,Integer index) 204 //譯:返回substring第一次在字符串中出現的位置,起始查詢字符串的位置爲index處 205 */ 206 System.debug('cd 在商品名稱中出現的位置:' + goodsName.indexOf('cd',2)); 207 208 /* 209 public Integer indexOfAny(String substring) 210 //譯:substring任意一個字符第一次在字符串中出現的位置 211 */ 212 System.debug('商品信息中select任意字符最早出現位置:' + goodsName.indexOfAny('select')); 213 214 /* 215 public Integer indexOfAnyBut(String substring) 216 //譯:返回substring任意一個字符不被包含的第一個位置,無則返回-1 217 */ 218 System.debug('商品信息中select任意一個字符最早不被包含的位置'+goodsName.indexOfAnyBut('select')); 219 220 /* 221 public Integer indexOfChar(int char) 222 //譯:返回字符串中char字符最早出現的位置 223 */ 224 Integer firstChar = goodsName.indexOfChar(55); 225 226 /* 227 public Integer indexOfDifference(String compareTo) 228 //譯:返回兩個字符串第一個不一樣位置的座標 229 */ 230 System.debug('商品名稱與abce字符串第一個不一樣的位置爲:' + goodsName.indexOfDifference('abce')); 231 232 /* 233 public Integer indexOfIgnoreCase(String substring) 234 //譯:返回substring在字符串中第一個出現的位置(不考慮大小寫) 235 */ 236 System.debug('商品名稱中第一個出現CD位置的爲(不分大小寫)' + goodsName.indexOfIgnoreCase('CD')); 237 238 /* 239 public Boolean isAllLowerCase() 240 //譯:字符串是否均爲小寫,若是是返回true,不然返回false 241 */ 242 System.debug('商品名稱中是否全是小寫: ' + goodsName.isAllLowerCase()); 243 244 /* 245 public Boolean isAllUpperCase() 246 //譯:字符串是否均爲大寫,若是是返回true,不然返回false 247 */ 248 System.debug('商品名稱中是否全是大寫:' + goodsName.isAllUpperCase()); 249 250 /* 251 public Boolean isAlpha() 252 //譯:若是當前全部字符均爲Unicode編碼,則返回true,不然返回false 253 */ 254 System.debug('商品名稱是否均爲Unicode編碼:' + goodsName.isAlpha()); 255 256 /* 257 public Boolean isAlphanumeric() 258 //譯:若是當前全部字符均爲Unicode編碼或者Number類型編碼,則返回true,不然返回false 259 */ 260 System.debug('商品名稱是否均爲Unicode或者Number類型編碼:' + goodsName.isAlphanumeric()); 261 262 /* 263 public Boolean isAlphanumericSpace() 264 //譯:若是當前全部字符均爲Unicode編碼或者Number類型或者空格,則返回true,不然返回false 265 */ 266 System.debug('商品名稱是否均爲Unicode,Number或者空格' + goodsName.isAlphanumericSpace()); 267 268 /* 269 public Boolean isAlphaSpace() 270 //譯:若是當前全部字符均爲Unicode或者空格,則返回true,不然返回false 271 */ 272 System.debug('商品名稱是否均爲Unicode,或者空格' +goodsName.isAlphaSpace()); 273 274 /* 275 public Boolean isAsciiPrintable() 276 //譯:若是當前全部字符均爲可打印的Asc碼,則返回true,不然返回false 277 */ 278 System.debug('商品名稱全部字符是否均爲可打印的Asc碼:' + goodsName.isAsciiPrintable()); 279 280 /* 281 public Boolean isNumeric() 282 //譯:若是當前字符串只包含Unicode的位數,則返回true,不然返回false 283 */ 284 System.debug('商品名稱全部字符是否均爲Unicode的位數:' + goodsName.isNumeric()); 285 286 /* 287 public Boolean isWhitespace() 288 //譯:若是當前字符只包括空字符或者空,則返回true,不然返回false 289 */ 290 System.debug('商品名稱全部字符只包括空字符或者空:' + goodsName.isWhitespace()); 291 292 /* 293 public static String join(Object iterableObj, String separator) 294 //譯:經過separator鏈接對象,通用於數組,列表等 295 */ 296 List<Integer> intLists = new Integer[] {1,2,3}; 297 String s = String.join(intLists,'/');//s = 1/2/3 298 299 300 /* 301 public Boolean lastIndexOf(String substring) 302 //譯:substring在字符串中最後出現的位置,不存在則返回-1 303 */ 304 System.debug('cd最後一次出現的位置:' + goodsName.lastIndexOf('cd')); 305 /* 306 public Boolean lastIndexOfIgnoreCase(String substring) 307 //譯:substring在字符串中最後出現的位置(忽略大小寫),不存在則返回-1 308 */ 309 System.debug('Cd最後一次出現的位置(不考慮大小寫):' + goodsName.lastIndexOfIgnoreCase('Cd')); 310 311 /* 312 public String left(Integer index) 313 //譯:獲取從零開始到index處的字符串 314 315 */ 316 System.debug('商品名稱前三個字符 : abc' + goodsName.left(3)); 317 318 /* 319 public String leftPad(Integer length) 320 //譯:返回當前字符串填充的空間左邊和指定的長度 321 */ 322 String s1 = 'ab'; 323 String s2 = s1.leftPad(4);//s2 = ' ab'; 324 /* 325 public Integer length() 326 //譯:返回字符串長度 327 */ 328 System.debug('商品名稱長度:' + goodsName.length()); 329 /* 330 public String mid(Integer startIndex,Integer length); 331 //譯:返回新的字符串,第一個參數爲起始位,第二個字符爲字符的長度//相似於substring 332 */ 333 System.debug('商品名稱截取字符串' + goodsName.mid(2,3)); 334 335 /* 336 public String normalizeSpace() 337 //譯:先後刪除空白字符 338 */ 339 String testNormalizeSpace = 'abc\t\n de'; 340 System.debug('經過normalizeSpace處理後的字符串爲:' + testNormalizeSpace.normalizeSpace()); 341 342 /* 343 public String remove(String substring) 344 //譯:移除全部特定的子字符串,並返回新字符串 345 public String removeIgnorecase(String substring) 346 //譯:移除全部特定的子字符串(忽略大小寫),並返回新字符串 347 */ 348 System.debug('商品名稱移除12後的字符串爲:' + goodsName.remove('12')); 349 350 /* 351 public String removeEnd(String substring) 352 //譯:當且僅當子字符串在後面移除子字符串 353 */ 354 System.debug('當顯示在商品名稱最後時移除:' + goodsName.removeEnd('顯示')); 355 356 /* 357 public String removeStatrt(String substring) 358 //譯:當且僅當子字符串在後面移除子字符串 359 */ 360 System.debug('當ab在商品名稱最前面時移除' + goodsName.removeStart('ab')); 361 362 /* 363 public String repeat(Integer numberOfTimes) 364 //譯:重複字符串numberOfTimes次數 365 */ 366 System.debug('重複商品名稱兩次的顯示爲:' + goodsName.repeat(2)); 367 368 /* 369 public String repeat(String separator, Integer numberOfTimes) 370 //譯:重複字符串numberOfTimes次,經過separator做爲分隔符 371 */ 372 System.debug('經過separator分隔符重複字符串兩次:' + goodsName.repeat('-',2)); 373 374 /* 375 public String replace(String target, String replacement) 376 //譯:將字符串中的target轉換成replacement 377 */ 378 System.debug('將商品名稱中ab替換成AB' + goodsName.replace('ab','AB')); 379 380 /* 381 public String replaceAll(String target,String replacement) 382 */ 383 System.debug('將商品名稱中ab所有替換成AB' + goodsName.replaceAll('ab','AB')); 384 385 /* 386 public String reverse() 387 //譯:字符串倒序排列 388 */ 389 System.debug('商品名稱倒序:' + goodsName.reverse()); 390 391 /* 392 public String right(Integer length) 393 //譯:從右查詢返回length的個數的字符串 394 */ 395 System.debug('返回商品名稱後三位字符:' + goodsName.right(3)); 396 397 /* 398 public String[] split(String regExp) 399 //譯:經過regExp做爲分隔符將字符串分割成數組 400 */ 401 String[] sonSplitString = goodsName.split('1');//經過1分割字符串 402 403 /* 404 public String[] split(String regExp, Integer limit) 405 //譯:經過regExp做爲分隔符將字符串分割成數組,limit顯示數組個數 406 */ 407 String [] sonSplitString1 = goodsName.split('1',2); 408 409 /* 410 public Boolean startsWith(string substring) 411 //譯:判斷字符串是否以substring開頭,若是是返回true,不是返回false 412 */ 413 System.debug('商品名稱是否以abcd開始:' + goodsName.startsWith('abcd')); 414 415 /* 416 public String substring(Integer length) 417 //譯:截取字符串固定長度 418 */ 419 System.debug('截取商品名稱前四個字符: ' + goodsName.substring(4)); 420 421 /* 422 public String toLowerCase() 423 //譯:將字符串轉換成小寫 424 */ 425 System.debug('商品名稱轉換成小寫:' + goodsName.toLowerCase()); 426 427 /* 428 public String toUpperCase() 429 //譯:將字符串轉換成大寫 430 */ 431 System.debug('商品名稱轉換成大寫:' + goodsName.toUpperCase()); 432 433 /* 434 public String trim() 435 //譯:去字符串左右空格 436 */ 437 System.debug('去空格後商品名稱:' + goodsName.trim()); 438 439 /* 440 public String uncapitalize() 441 //譯:將字符串第一個轉換成小寫 442 */ 443 System.debug('商品名稱第一個單詞轉換成小寫:' + goodsName.uncapitalize()); 444 445 /* 446 public static String valueOf(Object objectToConvert) 447 //譯:將Object類型轉換成String類型,其中Object類型包括如下: 448 // Date,DateTime,Decimal,Double,Integer,Long,Object 449 */ 450 System.debug('將Double類型轉換成String類型' + String.valueOf(3.45));
7)Boolean
Boolean類型聲明一個布爾類型,和java區別爲:Boolean類型變量有三個取值:true,false,null(default),因此使用Boolean類型聲明的時候必須賦予初始值,不然初始值爲null
二)時間日期類型
1)Datetime
Datetime類型聲明一個日期時間的對象,包含兩部分:日期,時間。由於salesforce通常製做global項目,因此日期時間通常取格林時間。Datetime無構造函數,若是實例化只能經過其靜態方法初始化。如下爲Datetime的部分主要方法:
1 Datetime nowDatetime = Datetime.now(); 2 Datetime datetime1 = Datetime.newInstance(2015,3,1,13,26,0); 3 String datetimeString = '2016-3-1 PM14:38'; 4 /* 5 Datetime datetime2 = Datetime.parse(datetimeString); 6 Datetime datetime3 = Datetime.valueOf(datetimeString); 7 */ 8 System.debug('經過初始化年月日時分秒獲得的Datetime,並轉換格式值:'+datetime1.format('yyyy-MM-dd HH:mm:ss')); 9 System.debug('當前日期時間:' + nowDatetime.format()); 10 /* 11 System.debug('經過parse方法初始化的datetime:' + datetime2.format()); 12 System.debug('經過valueOf方法初始化的datetime'+datetime3.format()); 13 */ 14 datetime1 = datetime1.addDays(1); 15 datetime1 = datetime1.addMonths(1); 16 datetime1 = datetime1.addYears(1); 17 datetime1 = datetime1.addHours(1); 18 datetime1 = datetime1.addMinutes(1); 19 datetime1 = datetime1.addSeconds(1); 20 System.debug('更改後的日期時間:' + datetime1.format('yyyy-MM-dd HH:mm:ss')); 21 Date date1 = datetime1.date(); 22 System.debug('datetime1對應的Date爲:'+date1.format()); 23 24 Date dateGmt = datetime1.dateGmt(); 25 System.debug('datetime1對應的DateGmt值爲:'+dateGmt.format()); 26 Integer year = datetime1.year(); 27 Integer yearGmt = datetime1.yearGmt(); 28 Integer month = datetime1.month(); 29 Integer monthGmt = datetime1.monthGmt(); 30 Integer day = datetime1.day(); 31 Integer dayGmt = datetime1.dayGmt(); 32 Integer dayOfYear = datetime1.dayOfYear(); 33 Integer dayOfYearGmt = datetime1.dayOfYearGmt(); 34 Integer hour = datetime1.hour(); 35 Integer hourGmt = datetime1.hourGmt(); 36 Integer minute = datetime1.minute(); 37 Integer minuteGmt = datetime1.minuteGmt(); 38 Integer second = datetime1.second(); 39 Integer secondGmt = datetime1.secondGmt(); 40 System.debug('year : '+ year + '\tyearGmt : ' + yearGmt); 41 System.debug('month : ' + month + '\tmonthGmt : '+ monthGmt); 42 System.debug('day : ' + day + '\tdayGmt : ' + dayGmt); 43 System.debug('hour : ' + hour + '\thourGmt : ' + hourGmt);//二者不一樣 一個爲14 Gmt爲6 44 System.debug('minute : ' + minute + '\tminuteGmt : ' + minuteGmt); 45 System.debug('second : ' + second + '\tsecondGmt : ' + secondGmt); 46 System.debug('dayOfYear : ' + dayOfYear + '\tdayOfYearGmt : ' + dayOfYearGmt); 47 System.debug('轉成本地日期並以長日期類型顯示:'+ datetime1.formatLong()); 48 Long timeL = datetime1.getTime(); 49 System.debug('轉成time類型的Long類型顯示爲:'+timeL.format()); 50 Datetime datetime5 = Datetime.newInstance(2016,4,2); 51 System.debug('datetime1與datetime2是否同一天:' + datetime1.isSameDay(datetime5));//true
2)Date
Date類型聲明一個日期的對象,Date能夠和Datetime相互轉換,主要須要掌握兩者關係以及相互轉換。
如下爲Date部分主要方法:
1 Date date2 = Date.today(); 2 System.debug('當前日期:' + date2.format()); 3 Date date3 = Date.newInstance(2016,3,1); 4 String dateString = '2016-3-1'; 5 Date date4 = Date.parse(dateString); 6 Date date5 = Date.valueOf(dateString); 7 System.debug('經過newInstance實例化:' + date3.format()); 8 System.debug('經過parse實例化:' + date4.format()); 9 System.debug('經過valueOf實例化:' + date5.format()); 10 11 date3 = date3.addMonths(1); 12 date3 = date3.addDays(1); 13 System.debug('date3的日期爲:' + date3.format()); 14 Integer year1 = date3.year(); 15 Integer month1 = date3.month(); 16 Integer day1 = date3.day(); 17 System.debug('year : ' + year1); 18 System.debug('month : ' + month1); 19 System.debug('day : ' + day1); 20 Integer dayOfYear1 = date3.dayOfYear(); 21 System.debug('dayOfYear : ' + dayOfYear1); 22 23 24 Integer daysBetween = date3.daysBetween(date4);//date4-date3 25 System.debug('date3和date4相差天數:' + daysBetween); 26 27 System.debug('date4和date5是否相同日期:'+date4.isSameDay(date5)); 28 29 System.debug('date3和date4相差月數:' + date3.monthsBetween(date4)); 30 31 System.debug('調用toStartOfMonth執行值:' + date3.toStartOfMonth().format());//返回本月第一天 32 /* 33 public Date toStartOfWeek() 34 //譯:返回本月第一個週日,若是本月1日非週日,則返回上月最晚的週日 35 */ 36 System.debug('調用toStartOfWeek執行值: ' + date3.toStartOfWeek().format());
3)Time
Time類型聲明一個時間的對象,對於時間須要考慮的是:由於中國時間和格林時間相差8小時,因此具體項目時若是是global項目須要考慮使用格林時間,即GMT時間。
三)集合類型
集合類型主要有三種,List,Set以及Map。其中三種均爲泛型方式,因此聲明變量時,直接帶上泛型。
1)List<T>
List表明一類的有序數據列表。數據序號從0開始。與JAVA不一樣的是:List是一個類,而且不存在ArrayList等子類。即實例化
eg:List<String> list1 = new List<String>();
List能夠經過自身構造函數實例化,也能夠經過數組進行實例化。
如下爲List主要方法:
注:set()方法在設置插入位置之前應確保長度大於須要插入的位置,不然將拋出異常。
1 List<String> lists = new String[]{'1','3'}; 2 List<String> list1 = new String[] {'5','4'}; 3 lists.set(0,'a'); 4 lists.add(0,'b'); 5 lists.add('2'); 6 lists.addAll(list1); 7 //lists.sort(); 8 for(String item : lists) { 9 System.debug('item : ' + item); 10 } 11 Iterator<String> iterator = lists.iterator(); 12 while(iterator.hasNext()) { 13 String item = iterator.next(); 14 System.debug('經過iterator遍歷:'+ item); 15 } 16 if(lists.size() > 0) { 17 Integer listSize = lists.size(); 18 lists.remove(listSize-1); 19 } 20 List<String> cloneList = lists.clone(); 21 for(Integer i=0;i<cloneList.size();i++) { 22 System.debug('cloneListItem : ' + cloneList.get(i)); 23 } 24 lists.clear(); 25 26 if(lists.size() > 0) { 27 for(String item : lists) { 28 System.debug('set item : ' + item); 29 } 30 } else { 31 System.debug('lists has already clear'); 32 }
2)Set<T>
Set表明一類數據的無序列表。與JAVA不一樣的是:Set是一個類,不存在HashSet等子類。即實例化
eg:Set<String> set1 = new Set<String>();
Set主要方法以下:
1 Set<String> set1 = new Set<String>(); 2 set1.add('1'); 3 set1.add('2'); 4 Set<String> set2 = set1.clone(); 5 Boolean isEquals = set1.equals(set2); 6 Boolean isContains = set1.contains('1'); 7 Integer setSize = set1.size(); 8 Iterator<String> iterator2 = set1.iterator(); 9 while(iterator2.hasNext()) { 10 System.debug('set item:' + iterator2.next()); 11 } 12 Boolean isEmpty = set1.isEmpty(); 13 //set1.remove('1'); 14 List<String> anotherList = new String[] {'1','3'}; 15 /* 16 public Boolean retainAll(List<Object> list) 17 //譯:set值爲list中和set重複的內容,若是沒有重複的內容,則set爲空 18 */ 19 set1.retainAll(anotherList); 20 Iterator<String> iterator3 = set1.iterator(); 21 while(iterator3.hasNext()) { 22 System.debug('set item via retainAll:' + iterator3.next());//1,List與Set公共部分 23 }
3)Map<K,V>
Map表明着鍵值對,與JAVA用法相似,區別爲Map是一個類,不是接口,不存在HashMap<K,V>等子類
Map主要方法以下:
1 Map<String,Object> map1 = new Map<String,Object>(); 2 map1.put('key1','value1'); 3 map1.put('key2','value2'); 4 map1.put('key3','value3'); 5 Boolean isContainsKey = map1.containsKey('key1'); 6 Map<String,Object> map2 = map1.clone(); 7 Boolean isMapEquals = map1.equals(map2); 8 Set<String> keySet = map1.keySet(); 9 String value1 = (String)map1.get('key1'); 10 List<String> valuesList = (List<String>)map1.values(); 11 Integer mapSize = map1.size();
四)運算及控制語句
運算與控制語句和JAVA基本相似,因此在這裏只是簡單介紹一下加強for循環
List<String> goodsList = new String[] {'衣服','褲子'}; for(String goods : goodsList) { System.debug(goods); }
詳細瞭解這些基本類型變量,集合以及相關方法請本身查看API。其實掌握一些主要的方法就能夠,其餘方法不用掌握大概清楚實現功能就好,具體須要的時候查看API看一下用法就能夠。若是有什麼問題,歡迎留言,共同探討。內容若是有寫的不正確地方,歡迎指正。下一篇將主要介紹sObject與簡單的DML操做。