格式字符串 [英] format string

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

问题描述



我正在尝试格式化字符串,如下所示:

从数据库中检索一个包含两个字段的小数据表然后循环获取字段值的记录......

Hi,
I am trying to format a string as follows:
A small datatable with two fields is retrieved from the database and then I loop through the records to get the value of the fields...

这就是我在下面的内容,但是你能看出为什么不是所有值都排在字段下面?请问如何解决?

This is what I have below but can you see why not all the values line up underneath the fields? and how to solve it please?

谢谢

例如:

我得到:

For example:
I get:

Field1   Field2

--------------------------------

你好   someri

jane   rose

browser   finchers 

Field1  Field2
--------------------------------
hello  someri
jane  rose
browsers  finchers 



if(dt.Rows.Count> 0)

{

  strFormat =" Field1" +" \t\t" +" Field2"; $
  strFormat + =" \ n" ;;

  strFormat + =" --------- -----------------------" ;;
$
  strFormat + =" \\\
" ;;


if (dt.Rows.Count > 0)
{
 strFormat = "Field1" + "\t\t" + "Field2";
 strFormat += "\n";
 strFormat += "--------------------------------";
 strFormat += "\n";

  foreach(dt.Rows中的DataRow dr)

        {

          string strValue1 = dr [" Field1"]。ToString();

          string strValue2 = dr [" Field2"]。ToString();

 foreach (DataRow dr in dt.Rows)
        {
          string strValue1 = dr["Field1"].ToString();
          string strValue2 = dr["Field2"].ToString();

          strFormat + = strValue1 +" \t\t" + strValue2;

                 strFormat + =" \ n" ;;

 }

}

          strFormat += strValue1 + "\t\t" + strValue2;
                 strFormat += "\n";
 }
}

推荐答案

要使用行写入字符串,必须使用已定义的字符转义符:" \\\\ nn"。

To have a string written in rows, you have to use defined characters escapes:"\r\n".

所以:

 strFormat += strValue1 + "\t\t" + strValue2 + "\r\n";

两个标签和最后一个回车符。

two tabs and a carriage return on the end.

或者您仍然可以使用string.Format方法,其中您准确定义了两个值(而不是制表符)之间的差异:

Or you can still use string.Format method, where you exactly define the difference between two values (instead of tab):

strFormat += string.Format("{0, -20} {1, -20} {2}", strValue1, strValue2, "\r\n");


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

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