C#编程 定义一个函数Get Word(string,int),返回参数一string中所指定下标值(参数二)的单词。
已知,参数一String中每个单词空格相隔。注C#编程例如:GetWord("Thisisadag.",2);返回“a”;GetWord("Thisisadog.",1,...
已知,参数一String中每个单词空格相隔。注C#编程
例如:Get Word("This is a dag." , 2);返回“a”;
Get Word ("This is a dog.", 1,2); 返回“is a”
很急 高手帮我啊 展开
例如:Get Word("This is a dag." , 2);返回“a”;
Get Word ("This is a dog.", 1,2); 返回“is a”
很急 高手帮我啊 展开
2个回答
展开全部
using System;
using System.Linq;
namespace Test
{
public class A
{
// 定义分隔符(分割单词,以空格为例)
private static char splitChar = ' ';
public static string GetWord(string str, int index)
{
string[] words = str.Split(splitChar); // 获得分割后的各个单词
if ((index < 0) || (index >= words.Count()))
{
return ""; // 如果下标超出范围,返回空字符串
}
else
{
return words[index];
}
}
// 姑且理解为参数1,2为取第1到第2个单词
// 如果你的本意是取第1个单词开始的2个单词,那么你自己改改
public static string GetWord(String str, int begin, int end)
{
string[] words = str.Split(splitChar); // 获得分割后的各个单词
if ((begin < 0) || (end >= words.Count()) || (begin > end))
{
return ""; // 如果下标超出范围,返回空字符串
}
else
{
string result = words[begin++];
for (; begin <= end; begin++)
{
result += splitChar + words[begin];
}
return result;
}
}
public static void Main(string[] args)
{
Console.WriteLine(GetWord("This is a dog.", 2));
Console.WriteLine(GetWord("This is a dog.", 1, 2));
}
}
}
using System.Linq;
namespace Test
{
public class A
{
// 定义分隔符(分割单词,以空格为例)
private static char splitChar = ' ';
public static string GetWord(string str, int index)
{
string[] words = str.Split(splitChar); // 获得分割后的各个单词
if ((index < 0) || (index >= words.Count()))
{
return ""; // 如果下标超出范围,返回空字符串
}
else
{
return words[index];
}
}
// 姑且理解为参数1,2为取第1到第2个单词
// 如果你的本意是取第1个单词开始的2个单词,那么你自己改改
public static string GetWord(String str, int begin, int end)
{
string[] words = str.Split(splitChar); // 获得分割后的各个单词
if ((begin < 0) || (end >= words.Count()) || (begin > end))
{
return ""; // 如果下标超出范围,返回空字符串
}
else
{
string result = words[begin++];
for (; begin <= end; begin++)
{
result += splitChar + words[begin];
}
return result;
}
}
public static void Main(string[] args)
{
Console.WriteLine(GetWord("This is a dog.", 2));
Console.WriteLine(GetWord("This is a dog.", 1, 2));
}
}
}
展开全部
class Test{
static void Main(string[] args){
string s=GetWord("This is a dog",2);
s=GetWord("This is a dog",1,2);
}
static string GetWord(string str, int st)
{
return GetWord(str, st, 1);
}
static string GetWord(string str,int st,int len){
if (st < 0 || len <= 0) return string.Empty;
str = ' '+str+' ';
int idx = 0;
int strLen=str.Length;
int st0 = strLen;
int end0 = strLen;
for (int i = st; i < strLen; i++)
{
if (str[i] != ' ') continue;
if (idx == st) st0 = i + 1;
if (idx == st + len) end0 = i;
idx++;
}
return str.Substring(st0, end0 - st0);
}
}
static void Main(string[] args){
string s=GetWord("This is a dog",2);
s=GetWord("This is a dog",1,2);
}
static string GetWord(string str, int st)
{
return GetWord(str, st, 1);
}
static string GetWord(string str,int st,int len){
if (st < 0 || len <= 0) return string.Empty;
str = ' '+str+' ';
int idx = 0;
int strLen=str.Length;
int st0 = strLen;
int end0 = strLen;
for (int i = st; i < strLen; i++)
{
if (str[i] != ' ') continue;
if (idx == st) st0 = i + 1;
if (idx == st + len) end0 = i;
idx++;
}
return str.Substring(st0, end0 - st0);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询