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

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

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  470 随笔 :: 0 文章 :: 22 评论 :: 30万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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   sunwugang  阅读(22)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示