c#每日一题(四)

VBDN 2005-04-04 01:48:22
编写一个控制台应用程序,接收一个长度大于3的字符串,完成下列功能:
1).输出字符串的长度。
2).输出字符串中第一个出现字母a的位置。
3).在字符串的第3个字符后面插入子串“hello”,输出新字符串。
4).将字符串“hello”替换为“me”,输出新字符串。
5).以字符“m”为分隔符,将字符串分离,并输出分离后的字符串。
...全文
645 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
xsxcjj 2005-04-21
  • 打赏
  • 举报
回复
if (str1.Length >= 3)
{
Console.WriteLine(str1.Length);
Console.WriteLine(str1.IndexOf('a'));
Console.WriteLine(str1.Insert(3,"hello"));
Console.WriteLine(str1.Replace("hello","me"));
Console.WriteLine(str1.Split('m'));
}
wangasp 2005-04-14
  • 打赏
  • 举报
回复
up
hyzoujinquan 2005-04-06
  • 打赏
  • 举报
回复
up
VBDN 2005-04-06
  • 打赏
  • 举报
回复
下边是VB.NET的代码:
Dim Hello As String
Dim Hello1 As String
Dim ArrList() As String
Hello = Console.ReadLine()
Console.WriteLine(Hello)
' 1)
Console.WriteLine("1." & Len(Hello))
' 2)
Console.WriteLine("2." & InStr(Hello, "a"))
' 3)
Hello1 = Mid(Hello, 1, 3) & "hello" & Mid(Hello, 4, Len(Hello) - 3)
Console.WriteLine("3." & Hello1)
' 4)
Hello1 = Replace(Hello1, "hello", "me")
Console.WriteLine("4." & Hello1)
' 5)
ArrList = Split(Hello1, "m")
Dim x
Console.WriteLine("5.")
For x = LBound(ArrList) To UBound(ArrList)
Console.WriteLine(ArrList(x))
Next
VBDN 2005-04-06
  • 打赏
  • 举报
回复
to fuchen_yan(fuchen)
============================
不要老丢不下VB,俺也是个VB爱好者,下边是俺用VB实现的代码:
Private Sub Command1_Click()
Dim Hello As String
Dim Hello1 As String
Dim ArrList() As String
Hello = InputBox("请输入一个长度大于3的字符串:");, "vbdn_abc")
Debug.Print , Hello
' 1)
Debug.Print "1.", Len(Hello)
' 2)
Debug.Print "2.", InStr(Hello, "a")
' 3)
Hello1 = Left(Hello, 3) & "hello" & Right(Hello, Len(Hello) - 3)
Debug.Print "3.", Hello1
' 4)
Hello1 = Replace(Hello1, "hello", "me")
Debug.Print "4.", Hello1
' 5)
ArrList = Split(Hello1, "m")
Dim x
Debug.Print "5.";
For x = LBound(ArrList) To UBound(ArrList)
Debug.Print , ArrList(x)
Next
End Sub
_-_-_-_- 2005-04-05
  • 打赏
  • 举报
回复
这个问题没有什么意义。。。但是大家当作是复习久知识嘛。。。
呵呵~~~~~~~
Ivony 2005-04-05
  • 打赏
  • 举报
回复
参考答案更像VB,并且一大堆的亢余代码。。。。
VBDN 2005-04-05
  • 打赏
  • 举报
回复
参考答案:
using System;
class Test6
{
public static void Main()
{
string str="";
while(str.Length<=3)
{
Console.Write("请输入一个长度大于3的字符串:");
str=Console.ReadLine();
}
//完成功能1
Console.WriteLine("字符串的长度为:{0}",str.Length);
//完成功能2
int i=str.IndexOf('a');
if (i>-1)
{
Console.WriteLine("第一个出现字母a的位置是:{0}",i);
}
else
{
Console.WriteLine("字符串中不包含字母a。");
}
//完成功能3
string str1=str.Insert(3,"hello");
Console.WriteLine("插入hello后的结果为:{0}",str1);
//完成功能4
string str2=str1.Replace("hello","me");
Console.WriteLine("将hello替换为me后的结果为:{0}",str2);
//完成功能5
string[] arr=str2.Split('m');
Console.WriteLine("以m为分隔符分离后的字符串有:");
for (int j=0;j<arr.Length;j++)
{
Console.WriteLine(arr[j]);
}
Console.ReadLine ();
}
}
wangasp 2005-04-05
  • 打赏
  • 举报
回复
练习!
linhaibo156 2005-04-05
  • 打赏
  • 举报
回复
是啊。我得多多学习学习这些
都忘得差不多了
niunj 2005-04-05
  • 打赏
  • 举报
回复
不错 是训练基本功的题目
xlkg_ss 2005-04-05
  • 打赏
  • 举报
回复
up
huangsuipeng 2005-04-04
  • 打赏
  • 举报
回复
ER,。,要不玩吃空格
一个STRING,把前头后头的空格吃掉,中间出现的空格不管几个,连续两个以上的以个代替
wingnal 2005-04-04
  • 打赏
  • 举报
回复
这问题有意义吗?
vrace 2005-04-04
  • 打赏
  • 举报
回复
最后那里打错了,是

foreach(string str in s.Split("m".ToCharArray())
{
...
}
vrace 2005-04-04
  • 打赏
  • 举报
回复
string s = Console.ReadLine();

Console.WriteLine("The length of this string is {0}", s.Length);

int index = s.IndexOf("a");
if(index >= 0)
{
Console.WriteLine("The index of first 'a' is {0}", index);
}
else
{
Console.WriteLine("'a' does not found.");
}

s = s.Insert(3, "hello");
Console.WriteLine(s);

s = s.Replace("hello", "me");
Console.WriteLine(s);

foreach(string str in s.Split("m"))
{
Console.WriteLine(str);
}
redlotoc 2005-04-04
  • 打赏
  • 举报
回复
能不能出些难点的问题
Ivony 2005-04-04
  • 打赏
  • 举报
回复
using System;

namespace MyConsole
{
class MyClass
{
/// </summary>
//与题目一样,我试图尽量展示C#的语法和特性。
[STAThread]
public static void Main( string[] args )
{
string str = null;

do//不常见的do...while循环,不要忘了C#的这个特性,有时可以用来优雅的解决一些问题
{
if ( str == null )//利用null值优雅的解决输入错误的提示。
str = "";
else
Console.WriteLine("输入错误,请输入三个字符以上。");

Console.WriteLine("请输入一个字符串:");
str = Console.ReadLine();
}
while( str.Length <= 3 );

Console.WriteLine();//无参WriteLine,也是很好用的一个东西。
Console.WriteLine( "字母'a'在您输入的字符串中所处位置为:{0}", str.IndexOf("a") );//WriteLine的这个重载是支持格式化字符串的

Console.WriteLine();
Console.WriteLine( str = str.Insert( 3, "hello" ) );//C语系的特色,赋值也是表达式,表达式就有返回值。虽然不推荐这样写,但有时候这种写法很优雅。
Console.WriteLine( str = str.Replace("hello","me") );

foreach ( string sstr in str.Split( 'm' ) )//foreach是对付数组最好的武器。
Console.Write( "{0}\t", sstr );
Console.WriteLine();

Console.WriteLine();
Console.WriteLine("按任意键继续");
Console.Read();//其实就是暂停了
}
}
}
xczgb 2005-04-04
  • 打赏
  • 举报
回复
希望出点经典题
BigIdiot628 2005-04-04
  • 打赏
  • 举报
回复
好帖,顶!
加载更多回复(8)

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧