You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何不使用内置函数(如String.replace)从字符串中移除指定子串?

How to Remove "blob" Substring Without Built-in Replace Functions

Got it, let's break down how to solve this problem properly. The task is to take an input string (like abcdblobefgblobhijk) and strip out every occurrence of the substring "blob"—and we can't use shortcut functions like String.replace() (or equivalent in other languages). Here's a straightforward, manual approach that works across most programming languages:

Core Approach

  • We'll iterate through each character of the input string, keeping track of how much of "blob" we've matched so far.
  • Use a mutable structure (like a list in Python, or a StringBuilder in Java) to build our result—this is more efficient than concatenating strings directly.
  • When we fully match "blob", we skip adding those characters to the result. If we have a partial match that breaks (e.g., we matched "blo" but the next character isn't "b"), we add the partial match to the result and reset our tracking.

Python Implementation

def remove_blob(input_str):
    target_substring = "blob"
    target_length = len(target_substring)
    result_chars = []
    current_match_pos = 0  # Tracks how many characters of "blob" we've matched

    for char in input_str:
        # Check if current character matches the next expected character in "blob"
        if char == target_substring[current_match_pos]:
            current_match_pos += 1
            # If we've matched the entire substring, reset to look for more occurrences
            if current_match_pos == target_length:
                current_match_pos = 0
        else:
            # If we had a partial match, add those characters to the result first
            if current_match_pos > 0:
                result_chars.extend(target_substring[:current_match_pos])
                current_match_pos = 0
            # Add the current character to the result
            result_chars.append(char)
    
    # After the loop, check if there's an unfinished partial match to add
    if current_match_pos > 0:
        result_chars.extend(target_substring[:current_match_pos])
    
    return ''.join(result_chars)

# Test with your sample input
sample_input = "abcdblobefgblobhijk"
print(remove_blob(sample_input))  # Output: abcdefghijk

Key Details Explained

  • Tracking Matches: current_match_pos starts at 0. Each time we hit a character that matches the next position in "blob", we increment this counter. When it reaches 4 (the length of "blob"), we know we've found a full match and reset the counter to look for the next one.
  • Handling Partial Matches: If we're in the middle of matching "blob" (e.g., we have "blo" but the next character is "x"), we add those "blo" characters to the result before adding the "x".
  • Final Cleanup: After the loop ends, we need to check if there's any partial match left (like if the string ends with "blo")—we add those characters to the result since they don't form a full "blob".

This approach is efficient (runs in O(n) time where n is the length of the input string) and avoids any built-in replace functions, which fits the problem requirements perfectly.

内容的提问来源于stack exchange,提问作者Udhai kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:22:07