如何从C#中的字符串中的不同单词中提取首字母 [英] How to extract first letters from different words in a string in c#

查看:436
本文介绍了如何从C#中的字符串中的不同单词中提取首字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提取字符串中每个单词的第一个字母.我做了很多谷歌搜索,仍然没有任何帮助.
例如,string text = "I Hate Programming";
所需的答案应类似于:

I want to extract first letter of each word in a string. I have done a lot of googling and still without any aid.
For example,string text = "I Hate Programming";
The desired answer should be like:

IHP

我知道你们非常好,我只是新来的.谢谢.

I know you guys are very good, I'm just new. thanks.

推荐答案

如果知道分隔符是一个空格,则可以执行以下操作.

If you know that your delimiter is a space, you can do the following.

string text = "my text here";
string firstLetters = "";

foreach(var part in text.split(' ')){
    firstLetters += part.substring(0,1);
}

基本上,您用空格字符分割字符串,然后使用每个单词的子字符串来抓取第一个字母.

Basically you split your string by the space character, and grab the first letter using substring of each word.

这篇关于如何从C#中的字符串中的不同单词中提取首字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆