1.replace对字符串进行替换;
2.判断字符串中是否含有大小写字母string stringA = "123 QQ qqq";
if(Regex.IsMatch(stringA, "[A-Z]"))
{
Console.WriteLine(stringA.ToLower());
}
2.判断字符串中是否含有大小写字母string stringA = "123 QQ qqq";
if(Regex.IsMatch(stringA, "[A-Z]"))
{
Console.WriteLine(stringA.ToLower());
}
如果你的需求不一样,可以自己修改一下。using System;
using System.Collections.Generic;namespace GetNumber
{
class Program
{
public static void Main(string[] args)
{
// string s = "sample 12";
// string s = "sample12 ";
// string s = "sample 12 ";
string s = "sample1 2";
// string s = "sample 12a";
// string s = "sample12b";
// string s = "sample 12 b8e";
//Console.WriteLine(string.Format("{0}: {1}", s, GetInt(s)));
foreach (string name in GetNameList(s, 5)) {
Console.WriteLine(name);
}
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
public static List<string> GetNameList(string Name,int count)
{
List<string> ret = new List<string>();
int num = GetInt(Name);
Name = Name.Trim();
string prefix = Name.Substring(0, Name.Length - num.ToString().Length);
for (int i = 0; i < count; ++i) {
ret.Add(prefix + (num + i + 1).ToString());
}
return ret;
}
public static int GetInt(string s)
{
int ret = 0;
s = s.Trim();
int len = s.Length;
int i;
for (i = len - 1; len >= 0; --i)
{
char c = (char) s[i];
Console.WriteLine(string.Format("{0}=>{1}", i, s[i])); if (!Char.IsDigit(c)) {
break;
}
}
Int32.TryParse(s.Substring(i + 1), out ret);
return ret;
}
}
}
思路是
1. 从右边开始判断字符串中的每个字符是否是数字(Char.IsDigit())
2. 如果非数字,跳出循环,并根据跳出的位置来截取最右边的字符串
3. 使用Int32.TryParse将截取的字符串转换为整数,方便后面对数字进行+1计算
4. 从用户输入的Name中去掉截取到的数字部分,作为新名字的前缀
5. 使用获取的新名字的前缀,通过对前面截取到的数字+1计算,组成新名字
真的感谢,我自己用了好多判断和循环语句,但总是达不到要的功能,这个代码我再去用心学习下。
string aa = "sample 12";
string bb = "sample1 2";
string cc = "sample 1 2"; var arr1 = bb.Split(new char[]{' '},StringSplitOptions.RemoveEmptyEntries).ToList();
var item1 = Convert.ToInt32(arr1.Last()); item1 += 1; arr1.Remove(arr1.Last());
arr1.Add(item1.ToString()); var dd = string.Join(" ",arr1);
public static int GetInt(string s)
{
int ret = 0;
s = s.Trim();
int len = s.Length;
int i;
if (!Char.IsDigit(s[len - 1]))
{
Int32.TryParse(s.Substring(len - 1), out ret);
}
else
{
for (i = len - 1; len > 0; i--)
{
if (!Char.IsDigit(s[i]))
{
ret = Int32.Parse(s.Substring(i+1));
}
}
}
return ret;
}
1. if (!Char.IsDigit(s[len - 1]))这句判断最后一个字符为非数字字符,然后再去Int32.TryParse。这段没有必要,因为原先的代码里面也是从最后一个字符判断(也就是你的else块里面),如果是非数字字符会break,跳出循环,最后也执行Int32.TryParse。你加了这个之后,等于最后一个字符串可能要判断两次(当最后一个字符是数字字符的时候)
2.你在else块里面的for循环里面,判断当字符为非数字字符的时候,你去Int32.Parse,也就是相当于非数字字符去转换成数字,这样肯定会出错的。另外循环没有break,就是要把整个字符串都遍历一遍。而你最初的需求是取出最后面的数字,遇到非数字字符的时候就可以结束判断了。
应该返回List<string> = {sample13,sample14,sample15}
判断字符串最后部分的数字时的原则:
1).忽略数字部分外的空格:即"sample 12","sample12 ","sample 12 "都认为数字是12;而"sample1 2"则认为是2;
2).最后一位不是空格也不是数字,则认为数字是0;如"sample 12a","sample12b","sample 12 b8e"都认为数字是0;*/
List<string> 生成 = new List<string>();
Func<string, int> 取数字 = delegate(string 信息)
{
string 字 = "";
int 量 = 信息.Length;
if (!Char.IsDigit(信息[--量]))
字 = "0";
else
do { if (Char.IsDigit(信息[量])) 字 = 字.Insert(0, 信息[量].ToString()); else break; } while (--量 >= 0);
return int.Parse(字);
};
Func<string[], int> 转数字 = delegate(string[] 信息)
{
int 判断 = 0, 检 = 0;/*如果转换成功则输出转换值否则等于设定值0*/
foreach (string 信 in 信息)
if (信 != "") { int.TryParse(信, out 检); ++判断; }/*包含对多空格处理*/
if (判断 < 2) 检 = 取数字(信息[0]);
return 检;
};
Func<string, int, List<string>> 符串最后部分的数字 = delegate(string 信息, int 数量)
{
int 数 = 0, 序 = 0;
string[] 拆分 = 信息.Split(' ');
序 = 数 = (拆分.Length > 1) ? 转数字(拆分) : 取数字(拆分[0]);
do
{
生成.Add(数 > 0 ? 信息.Replace(数.ToString(), (++序).ToString()) : 信息 + (++序).ToString());
} while (--数量 > 0);
return 生成;
};
符串最后部分的数字("sample11 2 ", 4);
foreach (string 信息 in 生成)
Console.WriteLine(信息);
do { if (Char.IsDigit(信息[量])) 字 = 字.Insert(0, 信息[量].ToString()); else break; } while (--量 > 0);
写完后想到的,之上代码中忘记改了,虽说目前测试不会发生错误,但知道了不改回来心理总有疙瘩很难受的,改一下心情很好.
不知道13楼要对信息.Split(' ');做什么,你不是只需要最后一个非空格的数字吗,对字符串中间的空格还是以空格处理的不是吗?就像你举的例子,"sample1 2"则认为是2。
你可以举个极端的例子开始的代码不能实现的,可以再看看怎么调整开始的代码纠正。
下面的代码你再测一下using System;
using System.Collections.Generic;namespace GetNumber
{
class Program
{
public static void Main(string[] args)
{
string s = "sample 12";
// string s = "sample12 ";
// string s = "sample 12 ";
// string s = "sample1 2";
// string s = "sample 12a";
// string s = "sample12b";
// string s = "sample 12 b8e";
//Console.WriteLine(string.Format("{0}: {1}", s, GetInt(s)));
Console.WriteLine(string.Format("Input: {0}", s));
PrintName(GetNameList(s, 3));
Console.WriteLine(string.Format("Input: {0}", s));
s = "sample 12 b8e";
PrintName(GetNameList(s, 3));
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
public static void PrintName(List<string> list)
{
foreach (string name in list)
{
Console.WriteLine(name);
}
}
public static List<string> GetNameList(string Name,int count)
{
List<string> ret = new List<string>();
int num = GetInt(Name);
Name = Name.Trim();
string prefix = Name.Substring(0, Name.Length - num.ToString().Length);
for (int i = 0; i < count; ++i)
{
ret.Add(prefix + (num + i + 1).ToString());
}
return ret;
}
public static int GetInt(string s)
{
int ret = 0;
s = s.Trim();
int len = s.Length;
int i;
for (i = len - 1; len >= 0; --i)
{
char c = (char) s[i];
//Console.WriteLine(string.Format("{0}=>{1}", i, s[i])); if (!Char.IsDigit(c))
{
break;
}
}
Int32.TryParse(s.Substring(i + 1), out ret);
return ret;
}
}
}