超级菜鸟叫救命!!

FWSH 2004-12-13 09:52:32
using System;

namespace test
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Test
{
static void SplitPath(string path,out string dir,out string name)
{
int i=path.Length;
while(i>0)
{
char ch=path[i-1];
if(ch=="\\"||ch=="/"||ch==":")break;
i--;
}
dir=path.Substring(0,i);
name=path.Substring(i);
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
string dir,name;
SplitPath("c:\\windows\\System\\hello.txt",out dir,out name);
Console.WriteLine(dir);
Console.WriteLine(name);
//
// TODO: 在此处添加代码以启动应用程序
//
}
}
}


错误提示:
E:\myPg\test\Class1.cs(16): 运算符“==”无法应用于“char”和“string”类型的操作数


以上程序的错误该如何修改.

...全文
146 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
icesnows 2004-12-13
  • 打赏
  • 举报
回复
if(ch=="\\"||ch=="/"||ch==":")break;
改为 if(ch=='\\'||ch=='/'||ch==':')break;

"c:\\windows\\System\\hello.txt"似乎写成@"c:\windows\System\hello.txt"更简洁,不是错误
icesnows 2004-12-13
  • 打赏
  • 举报
回复
双引号改成单引号就行了
chenyuming2004 2004-12-13
  • 打赏
  • 举报
回复
用"号就是字符串,
字符应该用'
hanyaocsdn 2004-12-13
  • 打赏
  • 举报
回复
看看这样好像可以吧.
using System;

namespace test
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Test
{
static void SplitPath(string path,out string dir,out string name)
{
int i=path.Length;
while(i>0)
{
char ch=path[i-1];
string ch1;
ch1=Convert.ToString(ch);
if(ch1=="\\"||ch1=="/"||ch1==":")break;
i--;
}
dir=path.Substring(0,i);
name=path.Substring(i);
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
string dir,name;
SplitPath("c:\\windows\\System\\hello.txt",out dir,out name);
Console.WriteLine(dir);
Console.WriteLine(name);
//
// TODO: 在此处添加代码以启动应用程序
//
}
}
}
zhy0101 2004-12-13
  • 打赏
  • 举报
回复
双引号改成单引号就行了
shang123guan 2004-12-13
  • 打赏
  • 举报
回复
ch=="\\"是什么?\\是两个char了,就是一个string啊!错误在这里
北京的雾霾天 2004-12-13
  • 打赏
  • 举报
回复
你的语法有误,改后如下:

using System;

namespace test
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Test
{
static void SplitPath(string path,out string dir,out string name)
{
int i=path.Length;
while(i>0)
{
char ch=path[i-1];
if(ch=='\\'||ch=='/'||ch==':')break;
i--;
}
dir=path.Substring(0,i);
name=path.Substring(i);
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
string dir,name;
SplitPath("c:\\windows\\System\\hello.txt",out dir,out name);
Console.WriteLine(dir);
Console.WriteLine(name);
//
// TODO: 在此处添加代码以启动应用程序
//
}
}
}

110,561

社区成员

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

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

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