String.IndexOf(Char)总是指向相同的Char吗?

时间:2014-03-11 10:09:21

标签: .net string .net-3.5 indexof

我正在查看一些现有代码(从C#转换而来)。它目前包含以下表达式:

Value.Substring(Value.IndexOf(symbol), 1)

其中symbol 来自Char的{​​{1}},Value。 (是的,它来自String。)

MSDN我所知,这与:

相同
For Each symbol As Char In Value

(会更快)。

如果情况不是这样,有人可以提出任何建议吗?

注意我确实看到此代码中的“缺陷”,这意味着它可能引用symbol.ToString symbol的早期版本而不是我们真正考虑的版本,但这是我的一部分问题:这真的很重要吗?

(显然,另一种解决方案是将代码重新编写为Value以保留对原始字符串的引用,以防万一。)

For i = 0 To Value.Length - 1: symbol = Value(i): ... Value.Substring(i, 1) ...

我已经重构了原来是重复表达的 ''' <summary> ''' This is a different Url Encode implementation since the default .NET one outputs the percent encoding in lower case. ''' While this is not a problem with the percent encoding spec, it is used in upper case throughout OAuth ''' </summary> ''' <param name="Value">The value to Url encode</param> ''' <returns>Returns a Url encoded string</returns> Public Function UrlEncode(ByVal Value As String) As String Dim result As New StringBuilder() For Each symbol As Char In Value If unreservedChars.IndexOf(symbol) <> -1 Then result.Append(symbol) Else 'some symbols produce > 2 char values so the system urlencoder must be used to get the correct data Dim hexAscW As String = String.Format("{0:X2}", CInt(AscW(symbol))) If hexAscW.Length > 3 Then result.Append(HttpUtility.UrlEncode(Value.Substring(Value.IndexOf(symbol), 1)).ToUpper()) Else result.Append("%"c & hexAscW) End If End If Next Return result.ToString() End Function

0 个答案:

没有答案