懂c#的高手进 高分悬赏

2025-02-24 22:17:13
推荐回答(6个)
回答1:

static void Main(string[] args)
{
Console.WriteLine("Please input the string to handle:");
string str = Console.ReadLine();
char[] src;

src = str.ToCharArray();

StringBuilder strb = new StringBuilder();

for (int i = 0; i < src.Length; i++)
{
if ( (i % 2 == 0) && (src[i] != ' ') && (src[i] != ',') && (src[i] != '.') && (src[i] != '"') && (src[i] != '\'') && (src[i] != '!') && (src[i] != '?') )
{
strb.Append(src[i]);
}
}

Console.WriteLine("Result:{0}", strb);
}

回答2:

懒得帮你写
string test = @" abcdefghijklmnopqrstuvwxyz,./;'[]`1234567890-=\ABCDEFGHIJKLMNOPQRSTUVWXYZ<>?:\""{}~!@#$%^&*()__MIDL___MIDL_itf_mshtml_0250_0001+|";
string test2 = "";
for (int i = 0; i < test.Length; i++)
{
if (i % 2 == 0)
{
test2 += test[i];
}
}
Console.WriteLine(test2);
Regex re = new Regex(@"[ ,\.\/\?\{\}\[\]:\""\'<>]");
test2 = re.Replace(test2, "");
Console.WriteLine(test2);

回答3:

删掉空格和标点,那就是字母和数字咯(要特殊符号自己加)
下面这个代码
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str = Console.ReadLine();
string str1="";

foreach( char chr in str)
if (char.IsLetter(chr) || char.IsDigit(chr))
str1=str1+chr;Console.WriteLine(str1 +"\r\n");
}
}
}

补充:忘了加奇数段了,随便了,楼主应该会的哈
就是判断str[i]%2的余数是几,或者循环的时候直接i+2

楼上可以直接
for (int i = 1; i < test.Length; i=i+2)
{
test2 += test[i];

回答4:

删除其中的空格,标点直接替换成空就行了···不用循环的!
找奇数还是要循环的!

回答5:

string[] x = { "aaaas , ;aslkj asd ...","sssssa;kfaljda;slkdf;alkj s",";ewpoip撒赖激,..发派就立刻" };
string[] outx= new string[x.Length /2 +1];
char[] rp = ",; ,。!·#¥%……—*(){}[]".ToCharArray();//自己添
for (int i = x.Length - 1; i >= 0; i--)
{
if (i % 2 == 0)
{//只添加下标是偶数的项
//替换 标点。。。等等,即非数字,英文,汉字的字符。

string resutlStr = System.Text.RegularExpressions.Regex.Replace(x[i], @"[^a-zA-Z0-9\u4e00-\u9fa5] ","",System.Text.RegularExpressions.RegexOptions.Singleline);

//添加到输出数组

outx[i / 2] = resutlStr;
}
}
foreach (string o in outx)
{
Console.WriteLine(o);
}

回答6:

觉得蛮好玩的,不信可以试试,规则可以自己加.
string gg = "sdfjl 239023 234 $-039234@_$";
string guize=" ,#,$,_,@,-";//规则自己加;
string hehe = clear(gg, guize,"奇数下标");//全套配齐了
Response.Write(hehe);

private string clear(string str,string guize,string type)
{
foreach (string s in guize.Split(','))
{
str = str.Replace(s,"").Trim();
}
string gg = "";
string ee = "";
for (int i = 0; i < str.Length; i++)
{
if (i % 2 == 0)
{
gg += str.Substring(i, 1);
}
else
{
ee += str.Substring(i, 1);
}
}
switch (type)
{
case "奇数下标":
return gg;
break;
case "偶数下标":
return ee;
break;
default:
return str;
break;
}
}