陳鍾誠的 C# 常用函式庫

程式作品

C 語言

Java

C#

JavaScript

常用函數

文字處理

遊戲程式

衛星定位

系統程式

資料結構

網路程式

自然語言

人工智慧

機率統計

資訊安全

等待完成

訊息

相關網站

參考文獻

最新修改

簡體版

English

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace ccc
{
    // ================================= 常用函數區 ================================
    public static class Log
    {
        static StreamWriter file = null;
        public static void open(String fileName)
        {
            file = new StreamWriter(fileName);
        }

        public static void log(this String o)
        {
            if (file != null)
                file.WriteLine(o.ToString());
//            Trace.WriteLine(str);
//            Console.WriteLine(str);
        }

        public static void log<T>(this IEnumerable<T> c)
        {
            log(String.Format("===== IEnumerable : {0} ======", c.GetType().Name));
            foreach (Object o in c)
                log(o.ToString());
        }

        public static void log<K, V>(this IDictionary<K, V> d)
        {
            log(String.Format("====== IDictionary : {0} ======", d.GetType().Name));
            foreach (KeyValuePair<K, V> p in d)
                log(p.Key.ToString() + ":" + p.Value.ToString());
        }

        public static void close()
        {
            if (file != null)
                file.Close();
        }
    }

    class MyRandom
    {
        public static Random r = new Random(7);

        public static int randomInt(int from, int to)
        {
            return from + r.Next() % (to - from + 1);
        }
        public static double randomDouble(double from, double to)
        {
            return from + r.NextDouble() * (to - from);
        }
    };

    public class CharSet
    {
        public static bool isChinese(char ch)
        {
            return (0x4E00 <= ch && ch <= 0x9FFF);
        }

        public static bool isAllChinese(String text)
        {
            foreach (char ch in text)
                if (!isChinese(ch)) return false;
            return true;
        }
    }

    public static class STR
    {
        public static String UTF8 = "utf-8";

        public static void Test()
        {
            Console.WriteLine(space(30) + "---");
        }

        public static String toStr<T>(this IEnumerable<T> c, String spliter)
        {
            StringBuilder rzStr = new StringBuilder();
            foreach (Object o in c)
                rzStr.Append(o.ToString()+spliter);
            return rzStr.ToString();
        }

        public static String toStr<K, V>(this IDictionary<K, V> d, String spliter)
        {
            StringBuilder rzStr = new StringBuilder();
            foreach (KeyValuePair<K, V> p in d)
                rzStr.Append(p.Key.ToString() + ":" + p.Value.ToString()+spliter);
            return rzStr.ToString();
        }

        public static String reverse(this String str)
        {
            StringBuilder rzStr = new StringBuilder();
            for (int i = str.Length - 1; i >= 0; i--)
                rzStr.Append(str[i]);
            return rzStr.ToString();
        }

        public static String replace(this String pStr, String fromPat, String toPat)
        {
            if (fromPat.Length == 0) return pStr;
            if (pStr.IndexOf(fromPat) < 0) return pStr;
            StringBuilder rzStr = new StringBuilder();
            int strIdx = 0, nextIdx;
            while ((nextIdx = pStr.IndexOf(fromPat, strIdx)) >= 0)
            {
                rzStr.Append(pStr.Substring(strIdx, nextIdx - strIdx));
                rzStr.Append(toPat);
                strIdx = nextIdx + fromPat.Length;
            }
            rzStr.Append(pStr.Substring(strIdx));
            return rzStr.ToString();
        }

        public static String expand(this String pText, String pMacros)
        {
            String[] macros = pMacros.Split('|');
            for (int i = 0; i < macros.Length; i++)
            {
                String name = head(macros[i], "=");
                String expand = tail(macros[i], "=");
                pText = replace(pText, name, expand);
            }
            return pText;
        }

        public static String head(this String pStr, String pSpliter)
        {
            int spliterPos = pStr.IndexOf(pSpliter);
            if (spliterPos < 0) return pStr;
            return pStr.Substring(0, spliterPos);
        }

        public static String tail(this String pStr, String pSpliter)
        {
            int spliterPos = pStr.IndexOf(pSpliter);
            if (spliterPos < 0) return "";
            return pStr.Substring(spliterPos + pSpliter.Length);
        }

        public static String last(this String pStr, String pSpliter)
        {
            int spliterPos = pStr.LastIndexOf(pSpliter);
            if (spliterPos < 0) return pStr;
            return pStr.Substring(spliterPos + 1);
        }

        public static String noLast(this String pStr, String pSpliter)
        {
            int spliterPos = pStr.LastIndexOf(pSpliter);
            if (spliterPos < 0) return pStr;
            return pStr.Substring(0, spliterPos);
        }

        public static String innerText(this String pText, String beginMark, String endMark)
        {
            int beginStart = pText.IndexOf(beginMark);
            if (beginStart < 0) return null;
            int beginEnd = beginStart + beginMark.Length;
            int endStart = pText.IndexOf(endMark, beginEnd);
            if (endStart < 0) return null;
            return pText.Substring(beginEnd, endStart - beginEnd);
        }

        public static String maxInnerText(this String pText, String beginMark, String endMark)
        {
            int beginStart = pText.IndexOf(beginMark);
            if (beginStart < 0) return null;
            int beginEnd = beginStart + beginMark.Length;
            int endStart = pText.LastIndexOf(endMark);
            if (endStart < 0) return null;
            return pText.Substring(beginEnd, endStart - beginEnd);
        }

        public static String space(int n)
        {
            return String.Format("{0," + n + "}", "");
        }

        public static String bytes2str(byte[] buf, int from, int len, String encode)
        {
            return Encoding.GetEncoding(encode).GetString(buf, from, len);
        }

        public static String bytes2str(byte[] buf, String encode)
        {
            return bytes2str(buf, 0, buf.Length, encode);
        }

        public static byte[] str2bytes(String str, String encode)
        {
            return Encoding.GetEncoding(encode).GetBytes(str);
        }

        public static String trim(this String str)
        {
            return str.Trim();
        }

        public static String nonull(this String str)
        {
            if (str == null) return "";
            return str;
        }
    }

    public class REGEX
    {
        public static String matchAt(String pText, int pFrom, String pPattern)
        {
            Regex r = new Regex(pPattern);
            Match m = r.Match(pText, pFrom);
            if (m.Index != pFrom)
                return null;
            return m.Groups[0].Value;
        }

        public static Match match(String pText, String pPattern)
        {
            return Regex.Match(pText, pPattern);
        }
    }

    class TextFile
    {
        public static String fileToText(String fileName)
        {
            StreamReader reader = new StreamReader(fileName);
            String text = reader.ReadToEnd();
            reader.Close();
            return text;
        }

        public static IEnumerable<String> lines(String fileName)
        {
            StreamReader reader = new StreamReader(fileName);
            while (true)
            {
                String line = reader.ReadLine();
                if (line == null)
                    break;
                yield return line;
            }
            reader.Close();
        }
    }
}

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License