如何删除所需字符串周围的文本?

时间:2022-12-22 02:29:56

I've looked online for this but not been able to find an answer unfortunately (sorry if there is something I have missed).

我在网上看了这个,但不幸的是找不到答案(对不起,如果我有什么遗漏的话)。

I have some code which filters out a specific string (which can change depending on what is read from the serial port). I want to be able to delete all of the characters which I am not using.

我有一些代码过滤掉一个特定的字符串(可以根据从串口读取的内容而改变)。我希望能够删除我没有使用的所有字符。

e.g. the string I want from the text below is "ThisIsTheStringIWant"

例如我想从下面的文字中找到的字符串是“ThisIsTheStringIWant”

efefhokiehfdThisIsTheStringIWantcbunlokew

Now, I already have a function with some code which will identify this and print it to where I want. However, as the comms could be coming in from multiple ports at any frequency, before printing the string to where I want it, I need to have a piece of code which will recognise everything I don't want and delete it from my buffer.

现在,我已经有了一些函数,其中包含一些代码,可以识别它并将其打印到我想要​​的位置。但是,由于通信可能以任何频率从多个端口进入,在将字符串打印到我想要​​的位置之前,我需要一段代码来识别我不想要的所有内容并将其从缓冲区中删除。

e.g. Using the same random text above, I want to get rid of the two random strings at the ends (which are before and after "ThisIsTheStringIWant" in the middle).

例如使用上面相同的随机文本,我想摆脱两端的两个随机字符串(在中间的“ThisIsTheStringIWant”之前和之后)。

efefhokiehfdThisIsTheStringIWantcbunlokew

I have tried using the highest voted answer from this question, however I can't find a way to delete the unwanted text before my wanted string. Remove characters after specific character in string, then remove substring?

我尝试过使用这个问题的最高投票答案,但是我找不到在我想要的字符串之前删除不需要的文本的方法。删除字符串中特定字符后的字符,然后删除子字符串?

If anyone can help, that would be great!

如果有人可以提供帮助,那就太好了!

Thanks!

谢谢!

Edit:

编辑:

Sorry, I should have probably made my question clearer.

对不起,我本可以提出更清楚的问题。

Any possible number of characters could be before and/or after the actual string I want, and as the string I want is coming from a serial port it will be different every time depending on what comms are coming in from the serial port. On my application I have a cell in a DGV called "Extract" and by typing in the first bit of the comms I am expecting (in this case, the extract would be This). But that will be different depending on what I am doing.

任何可能数量的字符都可以在我想要的实际字符串之前和/或之后,并且因为我想要的字符串来自串行端口,它每次都会有所不同,具体取决于从串行端口传入的通信。在我的应用程序中,我在DGV中有一个名为“Extract”的单元格,并输入我期望的通信的第一位(在这种情况下,提取将是This)。但这取决于我在做什么会有所不同。

1 个解决方案

#1


0  

Find the position of the string you want, delete from the beginning to the predecessor of that position, then delete everything from the length of your string to the end.

找到所需字符串的位置,从头开始删除该位置的前一个,然后删除从字符串长度到结尾的所有内容。

String: efefhokiehfdThisIsTheStringIWantcbunlokew

字符串:efefhokiehfdThisIsTheStringIWantcbunlokew

Step 1 - "ThisIsTheStringIWant" starts at position 13, so delete the first twelve, leaving...

第1步 - “ThisIsTheStringIWant”从第13位开始,所以删除前12位,留下......

String: ThisIsTheStringIWantcbunlokew

字符串:ThisIsTheStringIWantcbunlokew

Step 2 - "ThisIsTheStringIWant" is 20 characters long, so delete from character 21 to the length of the string, leaving:

第2步 - “ThisIsTheStringIWant”是20个字符长,所以从字符21删除字符串的长度,留下:

String: ThisIsTheStringIWant

字符串:ThisIsTheStringIWant

#1


0  

Find the position of the string you want, delete from the beginning to the predecessor of that position, then delete everything from the length of your string to the end.

找到所需字符串的位置,从头开始删除该位置的前一个,然后删除从字符串长度到结尾的所有内容。

String: efefhokiehfdThisIsTheStringIWantcbunlokew

字符串:efefhokiehfdThisIsTheStringIWantcbunlokew

Step 1 - "ThisIsTheStringIWant" starts at position 13, so delete the first twelve, leaving...

第1步 - “ThisIsTheStringIWant”从第13位开始,所以删除前12位,留下......

String: ThisIsTheStringIWantcbunlokew

字符串:ThisIsTheStringIWantcbunlokew

Step 2 - "ThisIsTheStringIWant" is 20 characters long, so delete from character 21 to the length of the string, leaving:

第2步 - “ThisIsTheStringIWant”是20个字符长,所以从字符21删除字符串的长度,留下:

String: ThisIsTheStringIWant

字符串:ThisIsTheStringIWant