如何格式化字符串 [英] How to format a string

查看:106
本文介绍了如何格式化字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要格式化字符串.

源字符串:123332qw332w2

结果字符串:123-332-qw3-32w2

有什么简单的方法可以像处理数字(String.Format)一样?


格式不固定.它在配置文件中给出,例如xxx-xxxxx-xxxxx.

可以以xxx-xxxxx-xxx之类的格式存储.一组值采用如上所述的单一格式.我需要格式化应基于上述给定的格式.格式xxx-xxxxx-xxx将成为可配置的.希望这能解释

Hi,

I need to format a string.

Source string: 123332qw332w2

Result string: 123-332-qw3-32w2

Is there any easy way to do it as we do for numbers(String.Format)?


Format is not fixed. It is given in the config file like xxx-xxxxx-xxxxx.

That can be stored in some format like xxx-xxxxx-xxx. A group of values take a single format like above.. I need the formatting should be based on a given format like above. Format xxx-xxxxx-xxx will be made configurable. Hope this explains

推荐答案

请参阅 ^ ]和 Regex.Replace [ ^ ]可能会帮助您:)

-KR
See this[^] and this[^]
Regex.Replace[^] might help you out :)

-KR


尝试一下
public void stringFormat()
       {
           var str = "123332qw332w2";
           var newStr = "";
           var increment = 1;
           for (int index = 3; index < 15; index += (3 + increment))
           {
               newStr = str.Insert(index, "-");
               str = newStr;

           }
             newStr;//final output
       }


它将显示您想要的输出
编码愉快:)


It will show your desired output
Happy coding :)


您应该使用正则表达式.

引用此链接:
http://msdn.microsoft.com/en-us/library/ms972966.aspx [ ^ ]
You should use regex.

Refered this link:
http://msdn.microsoft.com/en-us/library/ms972966.aspx[^]


这篇关于如何格式化字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆