字符串的应用1
来源:互联网 发布:数学 知乎 编辑:程序博客网 时间:2024/06/11 22:37
串联字符串
string userName = args[0];
string date = DateTime.Today.ToShortDateString();
string str = "hello" + userName + date;
System.Console.WriteLine(str);
string text = null;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 0; i < 100;i++ ) {
sb.AppendLine(i.ToString());
}
System.Console.WriteLine(sb.ToString());
System.Console.ReadKey();
移除指定字符串中的子字符串的各种方法。
class Program
{
string searchFor;
string replaceWith;
static void Main(string[] args)
{
Program p = new Program();
string s = "the mountins are behind";
s = s.Replace("m","o");
Console.WriteLine(s);
p.searchFor = "the";
p.replaceWith = "many";
s = Regex.Replace(s,p.searchFor,p.ReplaceMatchCase,RegexOptions.IgnoreCase);
Console.WriteLine(s);
s = s.Replace(' ','_');
Console.WriteLine(s);
//删除一个子串
string temp = "many_";
int i = s.IndexOf(temp);
if(i>=0){
s = s.Remove(i,temp.Length);
}
Console.WriteLine(s);
string s2 = " I'm wider than i need to be ";
temp = s2.Trim();//消除字符串两端空格
Console.WriteLine(temp);
Console.ReadKey();
}
string ReplaceMatchCase(Match m) {
if (Char.IsUpper(m.Value[0]) == true)
{
StringBuilder sb = new StringBuilder(replaceWith);
sb[0] = (Char.ToUpper(sb[0]));
return sb.ToString();
}
else { return replaceWith; }
}
}
若要使用数组表示法访问字符串中的各个字符,可以使用 StringBuilder 对象,该对象重载[] 运算符以提供对其内部字符缓冲区的访问。 也可以使用ToCharArray 方法将该字符串转换为一个字符数组。下面的示例使用 ToCharArray 创建该数组。然后修改该数组中的某些元素。 之后再调用采用一个字符数组作为输入参数的字符串构造函数来创建一个新字符串。
string str = "the quick brown fox";System.Console.WriteLine(str);
char[] chars = str.ToCharArray();
int animalIndex = str.IndexOf("fox");
if (animalIndex != -1) {
chars[animalIndex++] = 'c';
chars[animalIndex++] = 'a';
chars[animalIndex] ='t';
}
string str2 = new string(chars);
System.Console.WriteLine(str2);
System.Console.ReadKey();
比较字符串
// Internal strings that will never be localized.string root = @"C:\users";string root2 = @"C:\Users";// Use the overload of the Equals method that specifies a StringComparison.// Ordinal is the fastest way to compare two strings.bool result = root.Equals(root2, StringComparison.Ordinal);Console.WriteLine("Ordinal comparison: {0} and {1} are {2}", root, root2, result ? "equal." : "not equal.");// To ignore case means "user" equals "User". This is the same as using// String.ToUpperInvariant on each string and then performing an ordinal comparison.result = root.Equals(root2, StringComparison.OrdinalIgnoreCase);Console.WriteLine("Ordinal ignore case: {0} and {1} are {2}", root, root2, result ? "equal." : "not equal.");// A static method is also available.bool areEqual = String.Equals(root, root2, StringComparison.Ordinal);// String interning. Are these really two distinct objects?string a = "The computer ate my source code.";string b = "The computer ate my source code.";// ReferenceEquals returns true if both objects// point to the same location in memory.if (String.ReferenceEquals(a, b)) Console.WriteLine("a and b are interned.");else Console.WriteLine("a and b are not interned.");// Use String.Copy method to avoid interning.string c = String.Copy(a);if (String.ReferenceEquals(a, c)) Console.WriteLine("a and c are interned.");else Console.WriteLine("a and c are not interned.");// Output:// Ordinal comparison: C:\users and C:\Users are not equal.// Ordinal ignore case: C:\users and C:\Users are equal.// a and b are interned.// a and c are not interned.
string first = "Sie tanzen indie straBe.";
string second = "Sie tanzen indie Strasse";
Console.WriteLine(first);
Console.WriteLine(second);
// Store CultureInfo for the current culture. Note that the original culture
// can be set and retrieved on the current thread object.
System.Threading.Thread thread = System.Threading.Thread.CurrentThread;
System.Globalization.CultureInfo orginalCulture = thread.CurrentCulture;
//set the cultureinfo to en-us
thread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
int i = string.Compare(first,second,StringComparison.CurrentCulture);
Console.WriteLine("Comparing in {0},returns {1}",orginalCulture.Name,i);
//改变条件为荷兰语
thread.CurrentCulture = new System.Globalization.CultureInfo("de-DE");
i = string.Compare(first, second, StringComparison.CurrentCulture);
bool b = string.Equals(first,second,StringComparison.CurrentCulture);
Console.WriteLine(b==true ? "are" : "are not");
class programs {
static void Main(string[] args) {
string[] lines = new string[]
{
@"c:\public\textfile.txt",
@"c:\public\textFile.TXT",
@"c:\public\Text.txt",
@"c:\public\testfile2.txt"
};
Console.WriteLine("Non-sorted order.");
foreach(string s in lines){
Console.WriteLine(s);
}
Console.WriteLine("\n\rSorted order:");
Array.Sort(lines,StringComparer.Ordinal);
foreach(string s in lines){
Console.WriteLine(s);
}
string searchString = @"c:\public\TEXTFILE.TXT";
Console.WriteLine(searchString);
int result = Array.BinarySearch(lines,searchString,StringComparer.OrdinalIgnoreCase);
ShowWhere<string>(lines,result);
System.Console.ReadKey();
}
public static void ShowWhere<T>(T[] array,int index) {
if (index < 0)
{
index = ~index;
Console.WriteLine("Not found ,sorts between:");
if (index == 0)
Console.WriteLine("begining of array and");
else
Console.WriteLine("beginning of array and");
if (index == array.Length)
Console.WriteLine("end of array");
else
Console.WriteLine(array[index]);
}
else {
Console.WriteLine("Found at index{0}",index);
}
Console.ReadKey();
}
}
- 字符串的应用1
- 字符串的应用
- 字符串数组的应用
- 字符串排列组合的应用
- 字符串的各种应用
- 字符串的应用2
- 字符串的应用
- 字符串的应用
- 字符串的应用
- String字符串的应用
- 字符串的简单应用
- 字符串的属性应用
- 字符串的应用举例
- <字符串的应用>
- 字符串中单引号的应用
- NSString字符串的实际应用
- 学习转义字符串的应用
- Oracle 字符串相关的应用
- 第五届河南省大学生程序设计竞赛题解(第一次群赛兼妇女节专场)
- VMware vSphere 常见问题解答整理
- SeekBar使用5
- Shell编程中Shift的用法
- 遇到问题集锦(无c++基础 零起点小小小菜鸟亲身经历) 转自http://blog.csdn.net/qq592116366
- 字符串的应用1
- POJ 3189 Treats for the Cows(两种DP方法解决)
- servlet webservice 比较
- Android NDK开发Crash错误定位
- 数字字符转化整数
- 编译和安装的常见问题
- vim常用命令
- 使用Jquery实现图片轮播效果
- php 实现中英文倒序