2
0

String.hx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. /**
  23. The basic String class.
  24. A Haxe String is immutable, it is not possible to modify individual
  25. characters. No method of this class changes the state of `this` String.
  26. Strings can be constructed using the String literal syntax `"string value"`.
  27. String can be concatenated by using the `+` operator. If an operand is not a
  28. String, it is passed through `Std.string()` first.
  29. @see https://haxe.org/manual/std-String.html
  30. **/
  31. extern class String {
  32. /**
  33. The number of characters in `this` String.
  34. **/
  35. var length(default, null):Int;
  36. /**
  37. Creates a copy from a given String.
  38. **/
  39. function new(string:String):Void;
  40. /**
  41. Returns a String where all characters of `this` String are upper case.
  42. **/
  43. function toUpperCase():String;
  44. /**
  45. Returns a String where all characters of `this` String are lower case.
  46. **/
  47. function toLowerCase():String;
  48. /**
  49. Returns the character at position `index` of `this` String.
  50. If `index` is negative or exceeds `this.length`, the empty String `""`
  51. is returned.
  52. **/
  53. function charAt(index:Int):String;
  54. /**
  55. Returns the character code at position `index` of `this` String.
  56. If `index` is negative or exceeds `this.length`, `null` is returned.
  57. To obtain the character code of a single character, `"x".code` can be
  58. used instead to inline the character code at compile time. Note that
  59. this only works on String literals of length 1.
  60. **/
  61. function charCodeAt(index:Int):Null<Int>;
  62. /**
  63. Returns the position of the leftmost occurrence of `str` within `this` String.
  64. If `str` is the empty String `""`, then:
  65. * If `startIndex` is not specified or < 0, 0 is returned.
  66. * If `startIndex >= this.length`, `this.length` is returned.
  67. * Otherwise, `startIndex` is returned,
  68. Otherwise, if `startIndex` is not specified or < 0, it is treated as 0.
  69. If `startIndex >= this.length`, -1 is returned.
  70. Otherwise the search is performed within the substring of `this` String starting
  71. at `startIndex`. If `str` is found, the position of its first character in `this`
  72. String relative to position 0 is returned.
  73. If `str` cannot be found, -1 is returned.
  74. **/
  75. function indexOf(str:String, ?startIndex:Int):Int;
  76. /**
  77. Returns the position of the rightmost occurrence of `str` within `this`
  78. String.
  79. If `startIndex` is given, the search is performed within the substring
  80. of `this` String from 0 to `startIndex + str.length`. Otherwise the search
  81. is performed within `this` String. In either case, the returned position
  82. is relative to the beginning of `this` String.
  83. If `startIndex` is negative, the result is unspecified.
  84. If `str` cannot be found, -1 is returned.
  85. **/
  86. function lastIndexOf(str:String, ?startIndex:Int):Int;
  87. /**
  88. Splits `this` String at each occurrence of `delimiter`.
  89. If `this` String is the empty String `""`, the result is not consistent
  90. across targets and may either be `[]` (on Js, Cpp) or `[""]`.
  91. If `delimiter` is the empty String `""`, `this` String is split into an
  92. Array of `this.length` elements, where the elements correspond to the
  93. characters of `this` String.
  94. If `delimiter` is not found within `this` String, the result is an Array
  95. with one element, which equals `this` String.
  96. If `delimiter` is null, the result is unspecified.
  97. Otherwise, `this` String is split into parts at each occurrence of
  98. `delimiter`. If `this` String starts (or ends) with `delimiter`, the
  99. result `Array` contains a leading (or trailing) empty String `""` element.
  100. Two subsequent delimiters also result in an empty String `""` element.
  101. **/
  102. function split(delimiter:String):Array<String>;
  103. /**
  104. Returns `len` characters of `this` String, starting at position `pos`.
  105. If `len` is omitted, all characters from position `pos` to the end of
  106. `this` String are included.
  107. ```haxe
  108. "abcde".substr(3) // de
  109. ```
  110. If `pos` is negative, its value is calculated from the end of `this`
  111. String by `this.length + pos`. If this yields a negative value, 0 is
  112. used instead.
  113. ```haxe
  114. "abcde".substr(-2) // de
  115. ```
  116. If `len` is positive, up to `len` characters from the calculated position
  117. to the end of `this` String are included.
  118. ```haxe
  119. "abcde".substr(1, 2) // bc
  120. ```
  121. If `len` is negative, all characters from the calculated position to the end
  122. of `this` String, except the last `abs(len)` characters, are included.
  123. ```haxe
  124. "abcde".substr(0, -2) // abc
  125. "abcde".substr(1, -2) // bc
  126. ```
  127. If the calculated position + `len` exceeds `this.length`, the characters
  128. from that position to the end of `this` String are returned.
  129. **/
  130. function substr(pos:Int, ?len:Int):String;
  131. /**
  132. Returns the part of `this` String from `startIndex` to but not including `endIndex`.
  133. If `startIndex` or `endIndex` are negative, 0 is used instead.
  134. If `startIndex` exceeds `endIndex`, they are swapped.
  135. If the (possibly swapped) `endIndex` is omitted or exceeds
  136. `this.length`, `this.length` is used instead.
  137. If the (possibly swapped) `startIndex` exceeds `this.length`, the empty
  138. String `""` is returned.
  139. **/
  140. function substring(startIndex:Int, ?endIndex:Int):String;
  141. /**
  142. Returns the String itself.
  143. **/
  144. function toString():String;
  145. /**
  146. Returns the String corresponding to the character code `code`.
  147. If `code` is negative or has another invalid value, the result is
  148. unspecified.
  149. **/
  150. @:pure static function fromCharCode(code:Int):String;
  151. }