1、String.format()与String.valueOf()区别 && 2、string.split()

1、

<C#>

String.Format
将指定的 String 中的每个格式项替换为相应对象的值的文本等效项。
例子1:
int iVisit = 100;
string szName = "Jackfled";
Response.Write(String.Format("您的帐号是:{0} 。访问了 {1} 次.", szName, iVisit));

例子2:

c#把“23”格式成“000023”,用string.format,应该怎么写?

int i=23;
strNum=string.Format("{0:D6}", i); 

<java>

String.format()是根据一定的格式,将已经存在的字符串格式化
例如 System.out.println(String.format("今天的日期是:%tD", new java.util.Date()));
打印结果就是: 今天的日期是:03/22/08
该方法的第一个参数,就是格式,具体的格式说明,参考http://hi.baidu.com/zhangjianshe/blog/item/c21604b36b1764a3d9335a1a.html

String.valueOf()是用来将其他类型的数据转换为string型数据的
例如:System.out.println(String.valueOf('c'));
打印为:c

2、Split就是对字符串进行分割。

去除字符串WW中的“-”字符:

方法1:

string ww = "1-2-3-4-5-6-7-8-9";
        String[] zx = ww.Split('-');
        int o1;
        for (o1 = 0; o1 < zx.Length; o1++)
        {
            Response.Write(zx[o1]);
        }

方法2:

string a = "1-2-3-4-5-6-7-8-9";
        string[] arr = a.Split('-');
        string tempa = "";
        foreach (string ch in arr)
        {
            tempa += ch;
        }
        Response.Write(tempa);

其他示例:

namespace TestSplit
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            
string teststr = "abcdefabcdefabcdef";

            
string[] array = teststr.Split('b');

            
foreach (string i in array)
            
{
                Console.Write(i.ToString());
            }

            Console.ReadLine();
        }

    }

}

结果:acdefacdefacdfe

使用多个字符进行分割 看下面的例子:

namespace TestSplit
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            
string teststr = "abcdefabcdefabcdef";

            
string[] array = teststr.Split(new char[2]{'b','e'}); // 用c e 对字符串进行分割

            
foreach (string i in array)
            
{
                Console.Write(i.ToString());
            }

            Console.ReadLine();
        }

    }

}
     

结果:acdfacdfacdf

Software, the promoters of the progress of the times!------Shawn

原文地址:https://www.cnblogs.com/hekeboy/p/1270530.html