欢迎莅临 SUN WU GANG 的园子!!!

世上无难事,只畏有心人。有心之人,即立志之坚午也,志坚则不畏事之不成。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

StringHelper--字符串左右添加指定字符

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Configuration;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace HRMessageApp.Helper
 9 {
10     public class StringHelper
11     {
12         //定义一个用于保存静态变量的实例
13         private static StringHelper instance = null;
14         //定义一个保证线程同步的标识
15         private static readonly object locker = new object();
16         //构造函数为私有,使外界不能创建该类的实例
17         private StringHelper() { }
18         public static StringHelper Instance
19         {
20             get
21             {
22                 if (instance == null)
23                 {
24                     lock (locker)
25                     {
26                         if (instance == null) instance = new StringHelper();
27                     }
28                 }
29                 return instance;
30             }
31         }
32 
33         public string StrAppendChar(string str, char pChar = '"')
34         {
35             StringBuilder buf = new StringBuilder();
36             buf.Append(pChar);
37             buf.Append(str);
38             buf.Append(pChar);
39             return buf.ToString();
40         }
41 
42 
43         public string StrPadRightAndLeft(string str, char pChar = '"')
44         {
45             string one = str.PadLeft(str.Length + 1, pChar); ;
46             string two = one.PadRight(one.Length + 1, pChar);
47 
48             return two;
49         }
50 
51         public string StrLeftRightAppendChar(string str, char pChar = '"')
52         {
53             //string one = str.Replace(str.Substring(0, 1), pChar + str.Substring(0, 1));
54             string one = str.Replace(str, pChar + str);
55             string two = one.Replace(one, one + pChar);
56 
57             return two;
58         }
59 
60         public string StrLeftRightInsertChar(string str, char pChar='"')
61         {
62             string one = str.Insert(0, pChar.ToString()); //Insert() 在字串中指定索引位插入指定字符
63             string two = one.Insert(one.Length, pChar.ToString());
64 
65             return two;
66         }
67 
68         public string StrLeftRightPlusChar(string str, char pChar = '"')
69         {
70             return pChar + str + pChar;
71         }
72 
73         public string StrLeftRightAddChar(string str, char pChar = '"')
74         {
75             //string result = $"{pChar}{str}{pChar}";
76             return $"{pChar}{str}{pChar}"; 
77         }
78 
79 
80     }
81 }

补充方法:

 1       private static string MergeString(params string[] strs)
 2       {
 3           //string message = MergeString("\"", "abc", "\"");
 4           string result = string.Empty;
 5           foreach (string str in strs)
 6           {
 7               result = result + str;
 8           }
 9           return result;
10       }

 

posted on 2024-04-12 09:49  sunwugang  阅读(7)  评论(0编辑  收藏  举报