交换两个指定位置字符的4种方法
2011-07-15 11:39
211 查看
方法一:
static void Main()
...{
string s = "123456789";
SwapChar(ref s, 3, 6);
Console.Write(s.ToString());
}
static void SwapChar(ref string s, int i1, int i2)
...{
char temp = s[i1];
char temp1 = s[i2];
s = s.Remove(i1, 1).Insert(i1, temp1.ToString());
s = s.Remove(i2, 1).Insert(i2, temp.ToString());
}
方法二:
static void Main()
...{
string s = "123456789";
//SwapChar(s, 3, 6);
Console.Write(SwapChar(s, 3, 6).ToString());
}
static string SwapChar(string s, int p1, int p2)
...{
if ((p1 == p2) || ((p1 < 0) || (p2 < 0))) return s;
if ((p1 >= s.Length) || (p2 >= s.Length)) return s;
char[] vChars = s.ToCharArray();
vChars[p1] = (char)(vChars[p2] | (vChars[p2] = vChars[p1]) & 0);
return new string(vChars);
}
方法三:
static void Main()
...{
string s = "123456789";
Console.Write(SwapChar(s, 3, 6).ToString());
}
public static string SwapChar(string str, int a, int b)
...{
char[] newStr = str.ToCharArray();
newStr[a] = str[b];
newStr[b] = str[a];
return new string(newStr);
}
方法四:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
...{
class Program
...{
static void Main()
...{
string s = "123456789";
Console.Write(SwapChar(s, 3, 6).ToString());
}
static string SwapChar(string s, int p1, int p2)
...{
if (p1 > p2) ...{ int p = p1; p1 = p2; p2 = p; }
return Regex.Replace(s, "^(.{" + p1 + "})(.)(.{" + (p2 - p1 - 1) + "})(.)(.*)$", "$1$4$3$2$5");
}
}
}
static void Main()
...{
string s = "123456789";
SwapChar(ref s, 3, 6);
Console.Write(s.ToString());
}
static void SwapChar(ref string s, int i1, int i2)
...{
char temp = s[i1];
char temp1 = s[i2];
s = s.Remove(i1, 1).Insert(i1, temp1.ToString());
s = s.Remove(i2, 1).Insert(i2, temp.ToString());
}
方法二:
static void Main()
...{
string s = "123456789";
//SwapChar(s, 3, 6);
Console.Write(SwapChar(s, 3, 6).ToString());
}
static string SwapChar(string s, int p1, int p2)
...{
if ((p1 == p2) || ((p1 < 0) || (p2 < 0))) return s;
if ((p1 >= s.Length) || (p2 >= s.Length)) return s;
char[] vChars = s.ToCharArray();
vChars[p1] = (char)(vChars[p2] | (vChars[p2] = vChars[p1]) & 0);
return new string(vChars);
}
方法三:
static void Main()
...{
string s = "123456789";
Console.Write(SwapChar(s, 3, 6).ToString());
}
public static string SwapChar(string str, int a, int b)
...{
char[] newStr = str.ToCharArray();
newStr[a] = str[b];
newStr[b] = str[a];
return new string(newStr);
}
方法四:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
...{
class Program
...{
static void Main()
...{
string s = "123456789";
Console.Write(SwapChar(s, 3, 6).ToString());
}
static string SwapChar(string s, int p1, int p2)
...{
if (p1 > p2) ...{ int p = p1; p1 = p2; p2 = p; }
return Regex.Replace(s, "^(.{" + p1 + "})(.)(.{" + (p2 - p1 - 1) + "})(.)(.*)$", "$1$4$3$2$5");
}
}
}
相关文章推荐
- 交换两个指定位置字符的4种方法
- c语言:4种方法;交换两个整数的位置(包括引入第三方变量和不引入第三方变量)
- c语言:4种方法;交换两个整数的位置(包括引入第三方变量和不引入第三方变量)
- 两个操作字符串的方法:读取指定位置的字符和找出某个字符串的位置
- jQuery实现在textarea指定位置插入字符或表情的方法
- 交换两个变量的值(4种方法)
- leetcode之92. Reverse Linked List II(C++读错题版本,交换一个链表中指定的两个位置上的元素)
- 易语言指定从起始选择位置选中指定字符数量的方法
- JavaScript charCodeAt方法入门实例(用于取得指定位置字符的Unicode编码)
- jQuery实现在textarea指定位置插入字符或表情的方法
- javascript 在字符串中任意操作指定位置字符的方法
- 【codechef】交换字符串S的两个位置上的字符,求有多少对AB不相似
- javascript前端方法向输入域中光标所在位置或无光标最后插入指定字符
- JS中substring()方法(用于提取字符串中介于两个指定下标之间的字符)
- substring() 方法用于提取字符串中介于两个指定下标之间的字符。
- JavaScript indexOf方法入门实例(计算指定字符在字符串中首次出现的位置)
- JavaScript lastIndexOf方法入门实例(计算指定字符在字符串中最后一次出现的位置)
- JavaScript字符串对象charAt方法入门实例(用于取得指定位置的字符)
- JS之返回指定位置字符的charAt方法
- pyghon list指定位置插入字符的方法