vs2005 C#重写java.lang.String.substring()和java.lang.String.charAt()
1.C# and Java 关于substring的比较:
(1)先来看看有关JAVA关于substring的使用说明:
(2)再来看看C#关于Substring的描述:
(3)注意第二个参数,java是endIndex,而C#中则为length。
2.在vs2005中用C#重写其后的方法(这里定义为"_substring")为:
3.Java中关于charAt()的说明:
4.为了方便说明,这里结合Substring()和charAt()以一个解密算法的过程来详细说明:
(1)首先我们看看算法在Java中的应用:
(2)很多人都会在用C#改写charAt()时联想到IndexOf(),下面就以IndexOf()来改写举例:
经过调试,当判断的对象为数字时,两种方法返回值相同,但是当判断对象中包含字符时,如上的密文为“445cdeff4543534371”时,执行结果会大不一样,经过调试发现,Java中的.charAt()转化后的结果实际是ASCII码,所以在改写C#时也要将其做相应的转化,具体变化如下:
(3)分别在Eclipse和dot-Net下调试成功:
(Eclipse)
(dot-Net)
5.总结:
(1)C#中改写java.lang.String.substring()可用如下方法:
(2)C#中改写java.lang.String.charAt()可用如下方法:
(注:这里的".Substring()"为C#本身的方法定义)
(1)先来看看有关JAVA关于substring的使用说明:
public String substring(int beginIndex, int endIndex)
(2)再来看看C#关于Substring的描述:
public string Substring(int startIndex, int length);
(3)注意第二个参数,java是endIndex,而C#中则为length。
2.在vs2005中用C#重写其后的方法(这里定义为"_substring")为:
public static string _substring(string source,int start,int end)
{
string _strout = string.Empty;
try
{
_strout = source.Substring(0, end + 1);
}
catch(Exception e2)
{
Console.Write(e2.Message);
}
return _strout.Substring(start, _strout.Length - start - 1);
}
{
string _strout = string.Empty;
try
{
_strout = source.Substring(0, end + 1);
}
catch(Exception e2)
{
Console.Write(e2.Message);
}
return _strout.Substring(start, _strout.Length - start - 1);
}
3.Java中关于charAt()的说明:
4.为了方便说明,这里结合Substring()和charAt()以一个解密算法的过程来详细说明:
(1)首先我们看看算法在Java中的应用:
public static void main(String[] args) {
String mw = "445cdeff4543534371";//密文
String key = mw.substring(0, 3) + mw.substring(mw.length() - 3, mw.length());
String pass = mw.substring(3, mw.length() - 3);
int iKey1 = Integer.parseInt(key.substring(2, 3));
int iKey2 = Integer.parseInt(key.substring(1, 2));
int iKey3 = Integer.parseInt(key.substring(3, 4));
StringBuffer result = new StringBuffer();
for (int i = 0; i < pass.length(); i++) {
int tmp1 = pass.charAt(i);
int tmp2 = tmp1 + (iKey1 - iKey2 - iKey3);
result.append( (char) tmp2);
}
System.out.println(" \n result :"+result.toString());//明文输出
}
String mw = "445cdeff4543534371";//密文
String key = mw.substring(0, 3) + mw.substring(mw.length() - 3, mw.length());
String pass = mw.substring(3, mw.length() - 3);
int iKey1 = Integer.parseInt(key.substring(2, 3));
int iKey2 = Integer.parseInt(key.substring(1, 2));
int iKey3 = Integer.parseInt(key.substring(3, 4));
StringBuffer result = new StringBuffer();
for (int i = 0; i < pass.length(); i++) {
int tmp1 = pass.charAt(i);
int tmp2 = tmp1 + (iKey1 - iKey2 - iKey3);
result.append( (char) tmp2);
}
System.out.println(" \n result :"+result.toString());//明文输出
}
(2)很多人都会在用C#改写charAt()时联想到IndexOf(),下面就以IndexOf()来改写举例:
public static string decodePassword(string mw)
{
mw = mw.Trim();
string key = mw.Substring(0, 3) + _substring(mw, mw.Length - 3, mw.Length-1);
string pass = _substring(mw,3, mw.Length - 3);
int iKey1 = Int32.Parse(_substring(key,2, 3)); //Integer.parseInt(key.substring(2, 3));
int iKey2 = Int32.Parse(_substring(key, 1, 2)); //Integer.parseInt(key.substring(1, 2))
int iKey3 = Int32.Parse(_substring(key, 3, 4));
StringBuilder result = new StringBuilder();
for (int i = 0; i < pass.Length; i++)
{
int tmp1 = pass.IndexOf(i.ToString());
int tmp2 = tmp1 + (iKey1 - iKey2 - iKey3);
result.Append(tmp2.ToString());
}
return result.ToString();
}
//这里的"_substring()"方法即为上面改写的C#方法
{
mw = mw.Trim();
string key = mw.Substring(0, 3) + _substring(mw, mw.Length - 3, mw.Length-1);
string pass = _substring(mw,3, mw.Length - 3);
int iKey1 = Int32.Parse(_substring(key,2, 3)); //Integer.parseInt(key.substring(2, 3));
int iKey2 = Int32.Parse(_substring(key, 1, 2)); //Integer.parseInt(key.substring(1, 2))
int iKey3 = Int32.Parse(_substring(key, 3, 4));
StringBuilder result = new StringBuilder();
for (int i = 0; i < pass.Length; i++)
{
int tmp1 = pass.IndexOf(i.ToString());
int tmp2 = tmp1 + (iKey1 - iKey2 - iKey3);
result.Append(tmp2.ToString());
}
return result.ToString();
}
//这里的"_substring()"方法即为上面改写的C#方法
经过调试,当判断的对象为数字时,两种方法返回值相同,但是当判断对象中包含字符时,如上的密文为“445cdeff4543534371”时,执行结果会大不一样,经过调试发现,Java中的.charAt()转化后的结果实际是ASCII码,所以在改写C#时也要将其做相应的转化,具体变化如下:
int tmp1 = (int)char.Parse(pass.Substring(i, 1));
(3)分别在Eclipse和dot-Net下调试成功:
(Eclipse)
(dot-Net)
5.总结:
(1)C#中改写java.lang.String.substring()可用如下方法:
public static string _substring(string source,int start,int end)
{
string _strout = string.Empty;
_strout = source.Substring(0, end+1);
return _strout .Substring(start, _strout .Length - start -1);
}
{
string _strout = string.Empty;
_strout = source.Substring(0, end+1);
return _strout .Substring(start, _strout .Length - start -1);
}
(2)C#中改写java.lang.String.charAt()可用如下方法:
String.charAt(i) -> char.Parse(string.Substring(i, 1))