本文介绍了提取点A和点B之间的字符串的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从电子邮件中提取某些内容.电子邮件的常规格式始终为:

I am trying to extract something from an email. The general format of the email will always be:

blablablablabllabla hello my friend.

[what I want]

Goodbye my friend blablablabla

现在我做了:

                    string.LastIndexOf("hello my friend");
                    string.IndexOf("Goodbye my friend");

这将使我在开始之前有一个要点,在开始之后有一个要点.我可以使用哪种方法?我发现:

This will give me a point before it starts, and a point after it starts. What method can I use for this? I found:

String.Substring(Int32, Int32)

但这只是开始位置.

我可以使用什么?

推荐答案

子字符串采用起始索引(从零开始)和要复制的字符数.

Substring takes the start index (zero-based) and the number of characters you want to copy.

您需要做一些数学运算,如下所示:

You'll need to do some math, like this:

string email = "Bla bla hello my friend THIS IS THE STUFF I WANTGoodbye my friend";
int startPos = email.LastIndexOf("hello my friend") + "hello my friend".Length + 1;
int length = email.IndexOf("Goodbye my friend") - startPos;
string sub = email.Substring(startPos, length);

您可能希望将字符串常量放入 const string .

You probably want to put the string constants in a const string.

这篇关于提取点A和点B之间的字符串的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 15:15