C#中字符串的格式 [英] Formatton of string in C#

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

问题描述

你好,



我有不同的字符串,比如



  string  s =   11.2.1 ; 
string s1 = 53.12.1 ;
string s2 = 9.2.11 ;
string s3 = 1.12.1 ;





我希望输出像



< pre lang =c#> s = 110201;
s1 = 531201;
s2 = 090211;
s3 = 011201;





注意:字符串的长度始终小于9,前后总共有2个数字。(点)。



请给我最好最快的方法。



谢谢

解决方案

你可以参考的最简单的逻辑是按照这种方法,我在下面显示: -



  private  < span class =code-keyword> string  GetFormattedString( string  strInput)
{
string strOutput = string .Empty;
if (strInput.Contains( )。 ))
{
string [] arrInpt = strInput.Split( new char [] {' 。'});
if (arrInpt.Length == 3 ){
if (arrInpt [ 0 ]。长度== 1 ) strOutput = strOutput + 0 + arrInpt [ 0 < /跨度>]; else strOutput = strOutput + arrInpt [ 0 ];
if (arrInpt [ 1 ]。长度== 1 )strOutput = strOutput + 0 + arrInpt [ 1 ]; else strOutput = strOutput + arrInpt [ 1 ];
if (arrInpt [ 2 ]。长度== 1 )strOutput = strOutput + 0 + arrInpt [ 2 ]; else strOutput = strOutput + arrInpt [ 2 ];
}
}

return strOutput;
}





我们需要验证在传递之前作为输入传递给该方法的字符串,如果它是正确的格式与否则它将返回所需的字符串作为输出。



希望这对您有所帮助。


您好



检查这个最简单的代码来格式化字符串。希望这可以帮助您实现输出。



 私人 < span class =code-keyword> string  FormatString( string  inputString)
{
string formattedString = string .Empty;
尝试
{
string [] arrInput = inputString.Split (' 。');
foreach string value arrInput)
{
if value .Length.Equals( 1 ))
{formattedString + = 0 + value ; }
else
{formattedString + = value ; }
}
}
catch (例外情况)
{}
return formattedString;
}


请看::



  private   string  GetFormattedString( string  strInput )
{
return strInput.Split( new char [] {' 。'})[ 0 ]。PadLeft( 2 ' 0'
+ strInput.Split( new char [] {' 。'})[ 1 ]。PadLeft( 2 ' 0 '
+ strInput.Split( new char [] {' 。'})[ 2 ]。PadLeft( 2 ' 0');
}


Hello,

I have different strings like

string s = "11.2.1";
string s1 = "53.12.1";
string s2 = "9.2.11";
string s3 = "1.12.1";



And I want Output like

s = "110201";
s1 = "531201";
s2 = "090211";
s3 = "011201";



Note: String's length is always less than 9 and there is always 2 numbers before and after .(dot).

Please provide me the best and fastest approach.

Thanks

解决方案

The simplest logic you can refer is as per this method i have displayed below :-

private string GetFormattedString(string strInput)
        {
            string strOutput = string.Empty;
            if (strInput.Contains("."))
            {
                string[] arrInpt = strInput.Split(new char[] { '.' });
                if(arrInpt.Length == 3){
                    if (arrInpt[0].Length == 1) strOutput = strOutput + "0" + arrInpt[0]; else strOutput = strOutput + arrInpt[0];
                    if (arrInpt[1].Length == 1) strOutput = strOutput + "0" + arrInpt[1]; else strOutput = strOutput + arrInpt[1];
                    if (arrInpt[2].Length == 1) strOutput = strOutput + "0" + arrInpt[2]; else strOutput = strOutput + arrInpt[2];
                }
            }

            return strOutput;
        }



We need to validate the string which will be passed as input to this method before passing if it is in correct format or not then it will return the desired string as output.

Hope this will be of help to you.


Hi

Check this simplest code to format the string. Hope this will help you to achieve your output.

private string FormatString(string inputString)
        {
            string formattedString = string.Empty;
            try
            {
                string[] arrInput = inputString.Split('.');
                foreach (string value in arrInput)
                {
                    if (value.Length.Equals(1))
                    { formattedString += "0" + value; }
                    else
                    { formattedString += value; }
                }
            }
            catch (Exception ex)
            { }
            return formattedString;
        }


Please see this :-

private string GetFormattedString(string strInput)
        {
            return strInput.Split(new char[] { '.' })[0].PadLeft(2, '0')
                 + strInput.Split(new char[] { '.' })[1].PadLeft(2, '0')
                 + strInput.Split(new char[] { '.' })[2].PadLeft(2, '0');
        }


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

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