1 /* 2 * hunt-console eases the creation of beautiful and testable command line interfaces. 3 * 4 * Copyright (C) 2018-2019, HuntLabs 5 * 6 * Website: https://www.huntlabs.net 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module hunt.console.util.StringUtils; 13 14 import std.string; 15 import std.conv; 16 import hunt.text.Common; 17 import hunt.util.StringBuilder; 18 19 alias Character = char; 20 class StringUtils 21 { 22 public static string repeat(T)(string s, T n) 23 if(is(T == int) || is(T == ulong) || is(T == uint) || is(T == long)) 24 { 25 if (s is null) { 26 return null; 27 } 28 StringBuilder sb = new StringBuilder(); 29 for (int i = 0; i < n; i++) { 30 sb.append(s); 31 } 32 return sb.toString(); 33 } 34 35 public static string stripTags(string s) 36 { 37 return s.replace("\\<.*?\\>", ""); 38 } 39 40 public static int count(string word, Character ch) 41 { 42 int pos = cast(int)(word.indexOf(ch)); 43 44 return pos == -1 ? 0 : 1 + count(word.substring(pos+1),ch); 45 } 46 47 public static string join(string[] parts, string glue) 48 { 49 StringBuilder sb = new StringBuilder(); 50 for (int i = 0; i < parts.length; i++) { 51 sb.append(parts[i]); 52 if (i != parts.length - 1) { 53 sb.append(glue); 54 } 55 } 56 57 return sb.toString(); 58 } 59 60 public static string ltrim(string s) 61 { 62 return ltrim(s, ' '); 63 } 64 65 public static string ltrim(string s, char c) 66 { 67 int i = 0; 68 while (i < s.length && s[i] == c) { 69 i++; 70 } 71 return s.substring(i); 72 } 73 74 public static string rtrim(string s) 75 { 76 return rtrim(s, ' '); 77 } 78 79 public static string rtrim(string s, char c) 80 { 81 int i = cast(int)(s.length-1); 82 while (i >= 0 && s[i] == c) { 83 i--; 84 } 85 return s.substring(0,i+1); 86 } 87 88 public static string padRight(string string, int length, char padChar) 89 { 90 return padRight(string, length, (padChar)); 91 } 92 93 public static string padRight(string str, int length, string padString) 94 { 95 if (length < 1) { 96 return str; 97 } 98 return format("%-" ~ length.to!string ~ "s", str).replace(" ", padString); 99 } 100 101 public static string padLeft(string string, int length, char padChar) 102 { 103 return padLeft(string, length, (padChar)); 104 } 105 106 public static string padLeft(string str, int length, string padString) 107 { 108 if (length < 1) { 109 return str; 110 } 111 return format("%" ~ length.to!string ~ "s", str).replace(" ", padString); 112 } 113 114 /** 115 * Differs from String.split() in that it behaves like PHP's explode(): 116 * If s is the same string as c, the method returns an array with two elements both containing "" 117 */ 118 public static string[] split(string s, char c) 119 { 120 if (to!string(c) == s) { 121 return ["",""]; 122 } 123 124 return s.split(to!string(c)); 125 } 126 127 /** 128 * This string util method removes single or double quotes 129 * from a string if its quoted. 130 * for input string = "mystr1" output will be = mystr1 131 * for input string = 'mystr2' output will be = mystr2 132 * 133 * @param s value to be unquoted. 134 * @return value unquoted, null if input is null. 135 * 136 */ 137 public static string unquote(string s) 138 { 139 if (s !is null 140 && ((s.startsWith("\"") && s.endsWith("\"")) 141 || (s.startsWith("'") && s.endsWith("'")))) { 142 143 s = s.substring(1, s.length - 1); 144 } 145 146 return s; 147 } 148 }