StringExt.hx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package jvm;
  2. import java.NativeString;
  3. @:native("haxe.jvm.StringExt")
  4. class StringExt {
  5. public static function fromCharCode(code:Int):String {
  6. var a = new java.NativeArray(1);
  7. a[0] = code;
  8. return new String(a, 0, 1);
  9. }
  10. public static function charAt(me:String, index:Int):String {
  11. if (index >= me.length || index < 0)
  12. return "";
  13. else
  14. return java.lang.Character._toString((cast me : NativeString).charAt(index));
  15. }
  16. public static function charCodeAt(me:String, index:Int):Null<Int> {
  17. if (index >= me.length || index < 0)
  18. return null;
  19. else
  20. return cast((cast me : NativeString).charAt(index), Int);
  21. }
  22. public static function indexOf(me:String, str:String, startIndex:Null<Int>) {
  23. return
  24. if (startIndex == null) (cast me : NativeString).indexOf(str)
  25. else (cast me : NativeString).indexOf(str, startIndex);
  26. }
  27. public static function lastIndexOf(me:String, str:String, ?startIndex:Int):Int {
  28. if (startIndex == null || startIndex > me.length || startIndex < 0) {
  29. startIndex = me.length - 1;
  30. }
  31. return (cast me : NativeString).lastIndexOf(str, startIndex);
  32. }
  33. public static function split(me:String, delimiter:String):Array<String> {
  34. var ret = [];
  35. if (delimiter.length == 0) {
  36. for (i in 0...me.length) {
  37. ret.push(me.charAt(i));
  38. }
  39. } else {
  40. var start = 0;
  41. var pos = me.indexOf(delimiter, start);
  42. while (pos >= 0) {
  43. ret.push((cast me : NativeString).substring(start, pos));
  44. start = pos + delimiter.length;
  45. pos = me.indexOf(delimiter, start);
  46. }
  47. ret.push((cast me : NativeString).substring(start));
  48. }
  49. return ret;
  50. }
  51. public static function substr(me:String, pos:Int, ?len:Int):String {
  52. var len:Int = len == null ? me.length : len;
  53. if (pos != 0 && len < 0) {
  54. return "";
  55. }
  56. if (pos < 0) {
  57. pos = me.length + pos;
  58. if (pos < 0) {
  59. pos = 0;
  60. }
  61. } else if (len < 0) {
  62. len = me.length + len - pos;
  63. }
  64. if (pos + len > me.length) {
  65. len = me.length - pos;
  66. }
  67. if (pos < 0 || len <= 0) {
  68. return "";
  69. }
  70. return (cast me : NativeString).substring(pos, pos + len);
  71. }
  72. public static function substring(me:String, startIndex:Int, ?endIndex:Int):String {
  73. var endIndex:Int = endIndex == null ? me.length : endIndex;
  74. if (endIndex < 0) {
  75. endIndex = 0;
  76. } else if (endIndex > me.length) {
  77. endIndex = me.length;
  78. }
  79. if (startIndex < 0) {
  80. startIndex = 0;
  81. } else if (startIndex > me.length) {
  82. startIndex = me.length;
  83. }
  84. if (startIndex > endIndex) {
  85. var tmp = startIndex;
  86. startIndex = endIndex;
  87. endIndex = tmp;
  88. }
  89. return (cast me : NativeString).substring(startIndex, endIndex);
  90. }
  91. public static function toLowerCase(me:String):String {
  92. return me.toLowerCase();
  93. }
  94. public static function toUpperCase(me:String):String {
  95. return me.toUpperCase();
  96. }
  97. }