c# - 在严格的文本中获取文本的特定部分

标签 c# .net string c#-4.0

假设有一段文字如下:

string str = @"stackoverflow(stack:stackoverflow)overstackflow(over:stackoverflow )";

我想要粗体字段。 我想我必须在文本中找到“(”和“:”并获取它们之间的文本。不是吗?

有什么建议吗?

最佳答案

也许使用普通的 string 方法:

IList<String> foundStrings = new List<String>();
int currentIndex = 0;
int index = str.IndexOf("(", currentIndex);
while(index != -1)
{
    int start = index + "(".Length;
    int colonIndex = str.IndexOf(":", start);
    if (colonIndex != -1)
    {
        string nextFound = str.Substring(start, colonIndex - start);
        foundStrings.Add(nextFound);
    }
    currentIndex = start;
    index = str.IndexOf("(", currentIndex);
}

Demo

关于c# - 在严格的文本中获取文本的特定部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13968684/

相关文章:

c# - 监听另一个应用程序中的事件

c# - Windows Phone 8.1后台任务中的DispatcherTimer

c# - 在 .NET 中使用弱引用有什么好处?

.net - 在 TIBCO EMS(或其他 JMS)中,如何创建可扩展的请求/响应处理器?

c# - 如何故意让代码路径不在 C# 中返回 int 值

c# - 何时使用 TaskCreationOptions.LongRunning?

javascript - Blazor javascript interop - 从 js 到 .NET 的对象转换

python - 获取 python 字符串中字符集的位置

string - 将字符串转换为 Foo(类型字符串)

iphone - 有没有像 substringFromIndex 这样使用更少内存的函数?