简体   繁体   English

从 C# 中的给定字符串中提取子字符串

[英]Extract substrings from a given string in C#

I have a string from which i want to extract a required string as:我有一个字符串,我想从中提取所需的字符串:

"S101 Peter"
"S3282 Steve"

How to extract only the Names ie Peter and Steve from the above two strings.如何从上述两个字符串中仅提取姓名,即PeterSteve I worked out with Replace, Remove, TrimStart, IndexOf but couldn't find out?我使用 Replace、Remove、TrimStart、IndexOf 进行了计算,但找不到? Please help...请帮忙...

You want SubString :你想要SubString

var name = theString.SubString(theString.IndexOf(' ') + 1);
String S = "S101 Peter";
String S1 = S.split(" ")[1];

If you can ensure the pattern of "XXXX YYYY", you could probably just split it at the whitespace:如果您可以确保“XXXX YYYY”的模式,您可能只需将其拆分为空格:

string name = "S101 Peter".Split(' ')[1];

A simple way would be "S101 Peter".Split(' ')[1]一个简单的方法是"S101 Peter".Split(' ')[1]

You can also do.Split(' ')[1]你也可以做.Split(' ')[1]

string s = "S101 Peter";
string[] substrings = s.split(' ');
string result = substrings [1];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM