Str.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. public class Str
  6. {
  7. static Encoding asciiEncoding = Encoding.ASCII;
  8. public static Encoding AsciiEncoding
  9. {
  10. get { return Str.asciiEncoding; }
  11. }
  12. static Encoding utf8Encoding = Encoding.UTF8;
  13. public static Encoding Utf8Encoding
  14. {
  15. get { return Str.utf8Encoding; }
  16. }
  17. static Encoding uniEncoding = Encoding.Unicode;
  18. public static Encoding UniEncoding
  19. {
  20. get { return Str.uniEncoding; }
  21. }
  22. public static string ByteToHex3(byte[] data)
  23. {
  24. if (data.Length == 0)
  25. {
  26. return "";
  27. }
  28. return BitConverter.ToString(data) + "-";
  29. }
  30. public static string ByteToHex(byte[] data)
  31. {
  32. StringBuilder ret = new StringBuilder();
  33. foreach (byte b in data)
  34. {
  35. string s = b.ToString("X");
  36. if (s.Length == 1)
  37. {
  38. s = "0" + s;
  39. }
  40. ret.Append(s);
  41. }
  42. return ret.ToString();
  43. }
  44. public static byte[] HexToByte(string str)
  45. {
  46. try
  47. {
  48. List<byte> o = new List<byte>();
  49. string tmp = "";
  50. int i, len;
  51. str = str.ToUpper().Trim();
  52. len = str.Length;
  53. for (i = 0; i < len; i++)
  54. {
  55. char c = str[i];
  56. if (('0' <= c && c <= '9') || ('A' <= c && c <= 'F'))
  57. {
  58. tmp += c;
  59. if (tmp.Length == 2)
  60. {
  61. byte b = Convert.ToByte(tmp, 16);
  62. o.Add(b);
  63. tmp = "";
  64. }
  65. }
  66. else if (c == ' ' || c == ',' || c == '-' || c == ';')
  67. {
  68. }
  69. else
  70. {
  71. break;
  72. }
  73. }
  74. return o.ToArray();
  75. }
  76. catch
  77. {
  78. return new byte[0];
  79. }
  80. }
  81. public static string ByteToStr(byte[] data, Encoding enc)
  82. {
  83. try
  84. {
  85. return enc.GetString(data);
  86. }
  87. catch
  88. {
  89. return "";
  90. }
  91. }
  92. public static byte[] StrToByte(string str, Encoding enc)
  93. {
  94. try
  95. {
  96. return enc.GetBytes(str);
  97. }
  98. catch
  99. {
  100. return new byte[0];
  101. }
  102. }
  103. public static string[] GetLines(string str)
  104. {
  105. List<string> a = new List<string>();
  106. StringReader sr = new StringReader(str);
  107. while (true)
  108. {
  109. string s = sr.ReadLine();
  110. if (s == null)
  111. {
  112. break;
  113. }
  114. a.Add(s);
  115. }
  116. return a.ToArray();
  117. }
  118. public static string LinesToStr(string[] lines)
  119. {
  120. StringWriter sw = new StringWriter();
  121. foreach (string s in lines)
  122. {
  123. sw.WriteLine(s);
  124. }
  125. return sw.ToString();
  126. }
  127. public static bool IsEmptyStr(string str)
  128. {
  129. if (str == null || str.Trim().Length == 0)
  130. {
  131. return true;
  132. }
  133. else
  134. {
  135. return false;
  136. }
  137. }
  138. public static bool IsSplitChar(char c, string splitStr)
  139. {
  140. if (splitStr == null)
  141. {
  142. splitStr = StrToken.DefaultSplitStr;
  143. }
  144. foreach (char t in splitStr)
  145. {
  146. string a = "" + t;
  147. string b = "" + c;
  148. if (Util.StrCmpi(a, b))
  149. {
  150. return true;
  151. }
  152. }
  153. return false;
  154. }
  155. public static bool GetKeyAndValue(string str, out string key, out string value)
  156. {
  157. return GetKeyAndValue(str, out key, out value, null);
  158. }
  159. public static bool GetKeyAndValue(string str, out string key, out string value, string splitStr)
  160. {
  161. uint mode = 0;
  162. string keystr = "", valuestr = "";
  163. if (splitStr == null)
  164. {
  165. splitStr = StrToken.DefaultSplitStr;
  166. }
  167. foreach (char c in str)
  168. {
  169. switch (mode)
  170. {
  171. case 0:
  172. if (IsSplitChar(c, splitStr) == false)
  173. {
  174. mode = 1;
  175. keystr += c;
  176. }
  177. break;
  178. case 1:
  179. if (IsSplitChar(c, splitStr) == false)
  180. {
  181. keystr += c;
  182. }
  183. else
  184. {
  185. mode = 2;
  186. }
  187. break;
  188. case 2:
  189. if (IsSplitChar(c, splitStr) == false)
  190. {
  191. mode = 3;
  192. valuestr += c;
  193. }
  194. break;
  195. case 3:
  196. valuestr += c;
  197. break;
  198. }
  199. }
  200. if (mode == 0)
  201. {
  202. value = "";
  203. key = "";
  204. return false;
  205. }
  206. else
  207. {
  208. value = valuestr;
  209. key = keystr;
  210. return true;
  211. }
  212. }
  213. public static int StrCmpRetInt(string s1, string s2)
  214. {
  215. return s1.CompareTo(s2);
  216. }
  217. public static bool StrCmp(string s1, string s2)
  218. {
  219. return StrCmpRetInt(s1, s2) == 0 ? true : false;
  220. }
  221. public static int StrCmpiRetInt(string s1, string s2)
  222. {
  223. s1 = s1.ToUpper();
  224. s2 = s2.ToUpper();
  225. return StrCmpRetInt(s1, s2);
  226. }
  227. public static bool StrCmpi(string s1, string s2)
  228. {
  229. return StrCmpiRetInt(s1, s2) == 0 ? true : false;
  230. }
  231. public static bool IsStrInList(string str, params string[] args)
  232. {
  233. return IsStrInList(str, true, args);
  234. }
  235. public static bool IsStrInList(string str, bool ignoreCase, params string[] args)
  236. {
  237. foreach (string s in args)
  238. {
  239. if (ignoreCase)
  240. {
  241. if (StrCmpi(str, s))
  242. {
  243. return true;
  244. }
  245. }
  246. else
  247. {
  248. if (StrCmp(str, s))
  249. {
  250. return true;
  251. }
  252. }
  253. }
  254. return false;
  255. }
  256. public static bool IsNumber(string str)
  257. {
  258. str = str.Trim();
  259. foreach (char c in str)
  260. {
  261. if (c >= '0' && c <= '9')
  262. {
  263. }
  264. else
  265. {
  266. return false;
  267. }
  268. }
  269. return true;
  270. }
  271. public static string DateToString(DateTime dt)
  272. {
  273. if (dt.Ticks != 0)
  274. {
  275. return dt.ToString("yyyyMMdd HHmmss").Substring(2);
  276. }
  277. else
  278. {
  279. return "000000_000000";
  280. }
  281. }
  282. public static DateTime StringToDate(string str)
  283. {
  284. str = str.Replace("(JST)", "").Trim();
  285. try
  286. {
  287. return DateTime.Parse(str);
  288. }
  289. catch
  290. {
  291. return new DateTime(0);
  292. }
  293. }
  294. public static string ToSafeString(string str)
  295. {
  296. char[] chars =
  297. {
  298. ';', '?', '=', '<', '>', ':', '@', '%', '$', '\\', '/', '|', '\"', '\r', '\n',
  299. };
  300. string ret = str;
  301. foreach (char c in chars)
  302. {
  303. ret = ret.Replace(c, '_');
  304. }
  305. string ret2 = "";
  306. foreach (char c in ret)
  307. {
  308. bool b = false;
  309. if (c >= 0x00 && c <= 0x1f)
  310. {
  311. b = true;
  312. }
  313. if (c >= 0x7f && c <= 0xff)
  314. {
  315. b = true;
  316. }
  317. if (b == false)
  318. {
  319. ret2 += c;
  320. }
  321. else
  322. {
  323. ret2 += "_";
  324. }
  325. }
  326. return ret2;
  327. }
  328. public static byte[] Base64Decode(string src)
  329. {
  330. try
  331. {
  332. return System.Convert.FromBase64String(src);
  333. }
  334. catch
  335. {
  336. return null;
  337. }
  338. }
  339. public static string DecodeMime(string src)
  340. {
  341. string[] s = src.Split('?');
  342. byte[] b;
  343. if (s[2] == "B")
  344. {
  345. b = System.Convert.FromBase64String(s[3]);
  346. }
  347. else
  348. {
  349. throw new Exception("Bad Encode.");
  350. }
  351. string ret = System.Text.Encoding.GetEncoding(s[1]).GetString(b);
  352. if (s.Length >= 4)
  353. {
  354. string tmp = s[s.Length - 1];
  355. if (tmp.StartsWith("="))
  356. {
  357. ret += tmp.Substring(1);
  358. }
  359. }
  360. return ret;
  361. }
  362. public static bool HasNullChar(string str)
  363. {
  364. if (str.IndexOf('\0') != -1)
  365. {
  366. return true;
  367. }
  368. return false;
  369. }
  370. public static bool IsMailHeaderStr(string str)
  371. {
  372. if (str == null)
  373. {
  374. return false;
  375. }
  376. if (HasNullChar(str))
  377. {
  378. return false;
  379. }
  380. string[] sep = { ": " };
  381. string[] tokens = str.Split(sep, StringSplitOptions.RemoveEmptyEntries);
  382. if (tokens.Length >= 2)
  383. {
  384. return true;
  385. }
  386. return false;
  387. }
  388. public static bool IsStrForBase64(string str)
  389. {
  390. string b64str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/+=";
  391. foreach (char c in str)
  392. {
  393. bool b = false;
  394. foreach (char c2 in b64str)
  395. {
  396. if (c == c2)
  397. {
  398. b = true;
  399. break;
  400. }
  401. }
  402. if (b == false)
  403. {
  404. return false;
  405. }
  406. }
  407. return true;
  408. }
  409. }
  410. public class StrToken
  411. {
  412. string[] tokens;
  413. public string[] Tokens
  414. {
  415. get { return tokens; }
  416. }
  417. public string this[uint index]
  418. {
  419. get { return tokens[index]; }
  420. }
  421. public uint NumTokens
  422. {
  423. get
  424. {
  425. return (uint)Tokens.Length;
  426. }
  427. }
  428. const string defaultSplitStr = " ,\t\r\n";
  429. public static string DefaultSplitStr
  430. {
  431. get { return defaultSplitStr; }
  432. }
  433. public StrToken(string str)
  434. : this(str, null)
  435. {
  436. }
  437. public StrToken(string str, string splitStr)
  438. {
  439. if (splitStr == null)
  440. {
  441. splitStr = defaultSplitStr;
  442. }
  443. int i, len;
  444. len = splitStr.Length;
  445. char[] chars = new char[len];
  446. for (i = 0; i < len; i++)
  447. {
  448. chars[i] = splitStr[i];
  449. }
  450. tokens = str.Split(chars, StringSplitOptions.RemoveEmptyEntries);
  451. }
  452. }
  453. public class StrData
  454. {
  455. string strValue;
  456. public string StrValue
  457. {
  458. get { return strValue; }
  459. }
  460. public uint IntValue
  461. {
  462. get
  463. {
  464. return Util.StrToUInt(strValue);
  465. }
  466. }
  467. public ulong Int64Value
  468. {
  469. get
  470. {
  471. return Util.StrToULong(strValue);
  472. }
  473. }
  474. public StrData(string str)
  475. {
  476. if (str == null)
  477. {
  478. str = "";
  479. }
  480. strValue = str;
  481. }
  482. }