c# 學習筆記 - 將英文字串中,每個單字第一個字母變大寫
將一串英文字,每個單字第一個字母轉換成大寫
using System;
using System.Collections.Generic;
using System.Web;
/// <summary>
/// Summary description for Tools
/// </summary>
public class Tools
{
public Tools()
{
//
// TODO: Add constructor logic here
//
}
public static string UppercaseWords(string value)
{
char[] array = value.ToCharArray();
// Handle the first letter in the string.
if (array.Length >= 1)
{
if (char.IsLower(array[0]))
{
array[0] = char.ToUpper(array[0]);
}
}
// Scan through the letters, checking for spaces.
// ... Uppercase the lowercase letters following spaces.
for (int i = 1; i < array.Length; i++)
{
if (array[i - 1] == ' ')
{
if (char.IsLower(array[i]))
{
array[i] = char.ToUpper(array[i]);
}
}
}
return new string(array);
}
}
在別的Class中直接像以下的用法。
String str_Test = "test test test";
String str_Hello = Tools.UppercaseWords(str_Test );
using System;
using System.Collections.Generic;
using System.Web;
/// <summary>
/// Summary description for Tools
/// </summary>
public class Tools
{
public Tools()
{
//
// TODO: Add constructor logic here
//
}
public static string UppercaseWords(string value)
{
char[] array = value.ToCharArray();
// Handle the first letter in the string.
if (array.Length >= 1)
{
if (char.IsLower(array[0]))
{
array[0] = char.ToUpper(array[0]);
}
}
// Scan through the letters, checking for spaces.
// ... Uppercase the lowercase letters following spaces.
for (int i = 1; i < array.Length; i++)
{
if (array[i - 1] == ' ')
{
if (char.IsLower(array[i]))
{
array[i] = char.ToUpper(array[i]);
}
}
}
return new string(array);
}
}
在別的Class中直接像以下的用法。
String str_Test = "test test test";
String str_Hello = Tools.UppercaseWords(str_Test );
留言
張貼留言