Left
Dim S As String = "ABCDEFG"
Dim S2 As String = Microsoft.VisualBasic.Left(S, 3)
string S = "ABCDEFG";
string S2 = S.Substring(0,3);
Right
Dim S As String = "ABCDEFG"
Dim S2 As String = Microsoft.VisualBasic.Right(S, 3)
string S = "ABCDEFG";
string S2 = S.Substring(S.Length - 3,3);
Mid
Dim S As String = "ABCDEFG"
Dim S2 As String = Mid(S, 3, 3)
string S = "ABCDEFG";
string S2 = S.Substring(2,3);
============================
在 VB 裡的 3, 指的是第三個字元, 而C#裡是給字串的陣列Index, 從0開始, 所以第三個字元的位置會是 Index = 2 喔~ (0,1,2)
Instr
Dim S As String = "ABCDEFG"
Dim Pos As Integer = InStr(S, "C")
string S = "ABCDEFG";
int Pos = S.IndexOf("C");
=====================
在VB裡回傳的會是第幾個字元, 在C#裡回傳的會是字串的陣列位置, 所以 Pos 會差1喔!!
InstrRev
Dim S As String = "ABCDEFGABCDEFG"
Dim Pos As Integer = InStrRev(S, "C")
string S = "ABCDEFGABCDEFG";
int Pos = S.LastIndexOf("C");
=====================
在VB裡回傳的會是第幾個字元, 在C#裡回傳的會是字串的陣列位置, 所以 Pos 會差1喔!!
Space
Dim S As String = "A" & Space(3) & "B"
string S = "A" + new String(' ', 3) + "B";
Strdup
Dim S As String = "A" & StrDup(3, "C") & "B"
string S2 = "A" + new String('C', 3) + "B";
Replace
Dim S As String = "ABCDABCD"
Dim S2 As String = Replace(S, "A", "E")
string S = "ABCDABCD";
string S2 = S.Replace("A", "E");
=================
Dim S As String = "ABCABCABC"
Dim S2 As String = Replace(S, "A", "D", 1, 2)
string S = "ABCABCABC";
StringBuilder tmpS = new StringBuilder(S);
string S2 = tmpS.Replace("A", "D", 0, 2).ToString();
// 自行寫函數
// ====================================
string S = "ABCBCDBCDE";
S = Replacefun(S, "B", "", 2, 2);
private string Replacefun(string Source, string Find, string Replace, int FindNum, int RePlaceNum)
{
string result= Source;
for (int i = 0; i -1)
{
result = Source.Remove(StartPos, Find.Length).Insert(StartPos, Replace);
}
else
{
result = Source;
}
return result;
}
// ====================================
Choose
Dim S As String = Choose(3, "A", "B", "C", "D", "E", "F")
string S = new string[]{ "A", "B", "C", "D", "E", "F" }[2];
Format
Dim NowStr As String = Format(DateTime.Now, "yyyy/MM/dd HH:mm:ss.ffff")
string NowStr = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.ffff");
Len
Dim S As String = "ABCABCABC"
Dim i As Integer = Len(S)
string S = "ABCABCABC";
int i = S.Length;
Ucase/Lcase
Dim S As String = "abc"
Dim S2 As String = UCase(S)
Dim S3 As String = LCase(S2)
string S = "abc";
string S2 = S.ToUpper();
string S3 = S2.ToLower();
StrReverse
Dim S As String = "ABCDE"
Dim S2 As String = StrReverse(S)
string S = "ABCDE";
char[] tmpS = S.ToCharArray();
Array.Reverse(tmpS);
string S2 = new string(tmpS);
LTrim/RTrim/Trim
Dim S As String = " ABCDE "
Dim S2 As String = LTrim(S)
Dim S3 As String = RTrim(S)
Dim S4 As String = Trim(S)
string S = " ABCDE ";
string S2 = S.TrimStart();
string S3 = S.TrimEnd();
string S4 = S.Trim();
Asc
Dim S As String = "A"
Dim i As Integer = Asc(S)
string S = "A";
int i = S[0];
Chr
Dim i As Integer = 65
Dim S As String = Chr(i)
int i = 65;
string S = ((char)i).ToString();
Split
Dim S As String = "ABCDABCD"
Dim S2() As String = Split(S, "B")
string S = "ABCDABCD";
string[] S2 = S.Split('B');
=================
Dim S As String = "ABCDABCD"
Dim S2() As String = Split(S, "BC")
using System.Text.RegularExpressions;
string S = "ABCDABCD";
string[] S2 = Regex.Split(S, "BC");
LSet/Rset
Dim S As String = "BCD"
Dim S2 As String = LSet(S, 10)
Dim S3 As String = RSet(S, 10)
string S = "BCD";
string S2 = S.PadRight(10);
string S3 = S.PadLeft(10);
UBound
Dim a(4) As String
a(0) = "A"
a(1) = "B"
a(2) = "C"
a(3) = "D"
a(4) = "E"
Dim i As Integer = UBound(a)
string[] a = new string[5];
a[0] = "A";
a[1] = "B";
a[2] = "C";
a[3] = "D";
a[4] = "E";
int i = a.Length;
===============================
這裡附帶說明一下, 在VB裡對陣列的宣告數字 (4) 其長度是 5, 及 0~4 共五個, 但在 C# 裡的定義 [5] 所代表的就是長度5, 也是 0~4 而已喔!!
Weekday
Dim tmpNow As DateTime = DateTime.Now
Dim i As Integer = Weekday(tmpNow)
DateTime tmpNow = DateTime.Now;
int i = Convert.ToInt16(tmpNow.DayOfWeek) + 1;
======================
要注意 Weekday 函數和 DayOfWeek 會差 1 喔!!
CDate
Dim tmpNow As String = DateTime.Now
Dim tmpNow2 As Date = CDate(tmpNow)
string tmpNow = DateTime.Now.ToString();
DateTime tmpNow2 = Convert.ToDateTime(tmpNow);
Int
Dim i As Integer = Int(10.31)
int i = (int)10.31F;
Redim
Dim A() As String = New String() {"A", "B", "C"}
ReDim Preserve A(4)
A(3) = "D"
A(4) = "E"
string[] A = new string[]{"A","B","C"} ;
Array.Resize(ref A, 5);
A[3] = "D";
A[4] = "E";
IsNumeric/IsDate/Is........
Dim x As String = "A"
Dim y As String = "1"
Console.WriteLine(IsNumeric(x))
Console.WriteLine(IsNumeric(y))
string x = "A";
string y = "1";
Console.WriteLine(IsNumeric(x)) ;
Console.WriteLine(IsNumeric(y));
// IsNumeric 要自己寫...
private bool IsNumeric(string x)
{
try
{
double tmpx = Convert.ToDouble(x);
return true;
}
catch
{
return false;
}
}
IsDate 只要把 try { } 裡的 doule 和 Convert.ToDouble 分別改成 DateTime 和 Convert.ToDateTime 即可,
Is...... 以此類推...^^"
DateAdd:
Dim DateStart As Date = "1970/1/1"
Dim tmpToday As Date = DateAdd(DateInterval.Day, 1, DateStart)
Console.WriteLine(tmpToday)
DateTime DateStart = Convert.ToDateTime("1970/1/1");
DateTime tmpToday = DateStart.AddDays(1);
Console.WriteLine(tmpToday);
DateSerial:
Dim tmpDay As Date = DateSerial(2013, 12, 2)
DateTime tmpDay = new DateTime(2013, 12, 2);
公告版位
- Sep 16 Sun 2012 17:40
VB2010 字串函數/其它函數 in C#2010
全站熱搜
留言列表
發表留言