String.hx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (C)2005-2012 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. **/
  30. #if !macro
  31. import python.internal.StringImpl;
  32. #end
  33. @:coreApi
  34. @:native("str")
  35. extern class String {
  36. /**
  37. The number of characters in [this] String.
  38. **/
  39. var length(default,null) : Int;
  40. /**
  41. Creates a copy from a given String.
  42. **/
  43. function new(string:String) : Void;
  44. /**
  45. Returns a String where all characters of [this] String are upper case.
  46. Affects the characters [a-z]. Other characters remain unchanged.
  47. **/
  48. @:runtime public inline function toUpperCase() : String {
  49. return StringImpl.toUpperCase(this);
  50. }
  51. /**
  52. Returns a String where all characters of [this] String are lower case.
  53. Affects the characters [A-Z]. Other characters remain unchanged.
  54. **/
  55. @:runtime public inline function toLowerCase() : String {
  56. return StringImpl.toLowerCase(this);
  57. }
  58. /**
  59. Returns the character at position [index] of [this] String.
  60. If [index] is negative or exceeds [this].length, the empty String ""
  61. is returned.
  62. **/
  63. inline public function charAt(index : Int) : String
  64. {
  65. return StringImpl.charAt(this, index);
  66. }
  67. /**
  68. Returns the character code at position [index] of [this] String.
  69. If [index] is negative or exceeds [this].length, null is returned.
  70. To obtain the character code of a single character, "x".code can be used
  71. instead to inline the character code at compile time. Note that this
  72. only works on String literals of length 1.
  73. **/
  74. inline public function charCodeAt( index : Int) : Null<Int>
  75. {
  76. return StringImpl.charCodeAt(this, index);
  77. }
  78. /**
  79. Returns the position of the leftmost occurence of [str] within [this]
  80. String.
  81. If [startIndex] is given, the search is performed within the substring
  82. of [this] String starting from [startIndex]. Otherwise the search is
  83. performed within [this] String. In either case, the returned position
  84. is relative to the beginning of [this] String.
  85. If [str] cannot be found, -1 is returned.
  86. **/
  87. inline function indexOf( str : String, ?startIndex : Int ) : Int {
  88. return StringImpl.indexOf(this, str, startIndex);
  89. }
  90. /**
  91. Returns the position of the rightmost occurence of [str] within [this]
  92. String.
  93. If [startIndex] is given, the search is performed within the substring
  94. of [this] String from 0 to [startIndex]. Otherwise the search is
  95. performed within [this] String. In either case, the returned position
  96. is relative to the beginning of [this] String.
  97. If [str] cannot be found, -1 is returned.
  98. **/
  99. inline function lastIndexOf( str : String, ?startIndex : Int ) : Int {
  100. return StringImpl.lastIndexOf(this, str, startIndex);
  101. }
  102. /**
  103. Splits [this] String at each occurence of [delimiter].
  104. If [delimiter] is the empty String "", [this] String is split into an
  105. Array of [this].length elements, where the elements correspond to the
  106. characters of [this] String.
  107. If [delimiter] is not found within [this] String, the result is an Array
  108. with one element, which equals [this] String.
  109. If [delimiter] is null, the result is unspecified.
  110. Otherwise, [this] String is split into parts at each occurence of
  111. [delimiter]. If [this] String starts (or ends) with [delimiter}, the
  112. result Array contains a leading (or trailing) empty String "" element.
  113. Two subsequent delimiters also result in an empty String "" element.
  114. **/
  115. inline function split( delimiter : String ) : Array<String> {
  116. return StringImpl.split(this, delimiter);
  117. }
  118. /**
  119. Returns [len] characters of [this] String, starting at position [pos].
  120. If [len] is omitted, all characters from position [pos] to the end of
  121. [this] String are included.
  122. If [pos] is negative, its value is calculated from the end of [this]
  123. String by [this].length + [pos]. If this yields a negative value, 0 is
  124. used instead.
  125. If the calculated position + [len] exceeds [this].length, the characters
  126. from that position to the end of [this] String are returned.
  127. If [len] is negative, the result is unspecified.
  128. **/
  129. inline public function substr( pos : Int, ?len : Int ) : String
  130. {
  131. return StringImpl.substr(this, pos, len);
  132. }
  133. /**
  134. Returns the part of [this] String from [startIndex] to [endIndex].
  135. If [startIndex] or [endIndex] are negative, 0 is used instead.
  136. If [startIndex] exceeds [endIndex], they are swapped.
  137. If the (possibly swapped) [endIndex] is omitted or exceeds
  138. [this].length, [this].length is used instead.
  139. If the (possibly swapped) [startIndex] exceeds [this].length, the empty
  140. String "" is returned.
  141. **/
  142. inline function substring( startIndex : Int, ?endIndex : Int ) : String {
  143. return StringImpl.substring(this, startIndex, endIndex);
  144. }
  145. /**
  146. Returns the String itself.
  147. **/
  148. inline function toString() : String return StringImpl.toString(this);
  149. /**
  150. Returns the String corresponding to the character code [code].
  151. If [code] is negative or has another invalid value, the result is
  152. unspecified.
  153. **/
  154. public static inline function fromCharCode( code : Int ) : String {
  155. return StringImpl.fromCharCode(code);
  156. }
  157. }