回答
您可以使用Substring
和IndexOf
的组合。
var s = "Hello World";
var firstWord = s.Substring(0,s.IndexOf(" "));
但是,这不会给预期字词如果输入的字符串只有一个字,所以需要一个特殊情况。
var s = "Hello";
var firstWord = s.IndexOf(" ") > -1
? s.Substring(0,s.IndexOf(" "))
: s;
如果字符串只包含一个单词,例如如果** IndexOf **返回-1。 – 2010-08-31 08:32:13
string words = "hello world";
string [] split = words.Split(new Char [] {' '});
if(split.Length >0){
string first = split[0];
}
split.Length。堆栈溢出不会让我编辑它。 – crh225 2017-09-07 15:12:01
一种方法是查找字符串中的空间,使用空间的位置,以获得的第一个字:
int index = s.IndexOf(' ');
if (index != -1) {
s = s.Substring(0, index);
}
另一种方法是使用正则表达式寻找一个字边界:
s = Regex.Match(s, @"(.+?)\b").Groups[1].Value;
你可以试试:
string s = "Hello World";
string firstWord = s.Split(' ').First();
Ohad Schneider's评论是正确的,所以你可以简单地要求First()
元素,因为总会有至少一个元素。
有关是否使用First()
或进一步信息FirstOrDefault()
你可以学到更多here
的answer of Jamiec是最有效的,如果要拆分只对空间。但是,只是为了不同的缘故,这里的另一个版本:
var FirstWord = "Hello World".Split(null, StringSplitOptions.RemoveEmptyEntries)[0];
作为奖励,这也将认识到all kinds of exotic whitespace characters并忽略多个连续的空格字符(实际上这将削减从结果前/后空格) 。
请注意,它也会将符号计数为字母,所以如果您的字符串是Hello, world!
,它将返回Hello,
。如果你不需要,那么在第一个参数中传递一个分隔字符数组。
但是,如果你希望它是在世界上的每一种语言100%万无一失,那么它会变得艰难......
从MSDN网站(http://msdn.microsoft.com/en-us/library/b873y76a.aspx)
string words = "This is a list of words, with: a bit of punctuation" +
"\tand a tab character.";
string [] split = words.Split(new Char [] {' ', ',', '.', ':', '\t' });
if(split.Length > 0)
{
return split[0];
}
无耻被盗处理各种不同的空白字符,空字符串和单个字符串。
而不是做Split
所有的字符串,限制你的分割数的2。使用以count为参数的过载。使用String.Split Method (Char[], Int32)
string str = "hello world";
string firstWord = str.Split(new[]{' '} , 2).First();
Split
总是返回至少一个元素的数组所以无论是.[0]
或First
就够了。
我在我的代码中使用了这个函数。它提供了一个选项,以大写第一个单词或每个单词。
public static string FirstCharToUpper(string text, bool firstWordOnly = true)
{
try
{
if (string.IsNullOrEmpty(text))
{
return text;
}
else
{
if (firstWordOnly)
{
string[] words = text.Split(' ');
string firstWord = words.First();
firstWord = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(firstWord.ToLower());
words[0] = firstWord;
return string.Join(" ", words);
}
else
{
return System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(text.ToLower());
}
}
} catch (Exception ex)
{
Log.Exc(ex);
return text;
}
}
- 1. 如何在Excel中引入数字左侧的字符串
- 2. 从vb6中的字符串的右侧和左侧删除某个字符(TrimChar)
- 3. LINQ:字符串函数从左边
- 4. 以字符一个字符的左侧
- 5. 删除Python字符串左侧的字符?
- 6. MySQL:由分隔符字符串分割的字符串的左侧部分?
- 7. 从左侧和右侧修剪字符串
- 8. C字符串函数
- 9. C中的反向字符串函数
- 10. 基于左侧字符计数?
- 11. SQL查询删除字段中的左侧和右侧字符
- 12. 在sql中获取字符串左侧分隔符的所有字符
- 13. 左侧和右侧并排放置两个UILabels,不知道左侧标签中文本的字符串长度
- 14. python中的函数,证明字符串(左,中,右)
- 15. CMake的函数字符串转换为C字符串字面
- 16. 左在C#中的字符串的字符
- 17. 使用malloc替换字符串中子字符串的C函数 - 无字符串函数
- 18. 在C++左边的分割字符串
- 19. 函数用于将字符串中的字符数从右到左传输到目标字符串
- 20. 在if语句的左侧使用字符串的值
- 21. 将函数中的字符串与变量连接起来时出现无效的左侧参数错误
- 22. C#4.0函数检查字符串中的前四个字符
- 23. 字符串上的c函数
- 24. C++字符串的tolower函数
- 25. C++数组字符串函数
- 26. 如何删除此字符串的左侧?
- 27. 如何移动给定数字索引左侧的数组(或c字符串)的元素
- 28. SQL从大字符串中提取字符串在关键字搜索左侧的列中
- 29. C# - 左侧ListView列?
- 30. 从左侧格式化C#中的数字
空格是你想分开单词的唯一字符吗?那么tab,换行符和回车呢? – cwap 2010-08-31 08:31:28
延伸cwap的评论:标点怎么样? “你好,世界”? – lalli 2010-08-31 08:44:58