1
string input = "\"Hello, World!\" ! \"Some other string\"";

Hello, I am having a problem with finding a solution to this. You see, I want to Split the string in half by the ! separating the two "fake strings" inside the string. I am aware that I can use String.Split(), but what if there is an exclamation mark inside the "fake string"?

Would appreciate if anyone could help.

3
  • what do you mean by "fake string" and what is your desired result? Commented Sep 5, 2021 at 13:39
  • You could use regular expressions to extract each internal or "fake" string. I'm assuming by "fake string" you mean the text inside the escaped double quotes \". Commented Sep 5, 2021 at 13:43
  • Its better to use any other separator then !. Commented Sep 5, 2021 at 13:45

3 Answers 3

2

You could use a regex: "(.*)" ! "(.*)"

https://regex101.com/r/4uBspp/1

Sign up to request clarification or add additional context in comments.

Comments

2

Assuming the seperator will always be formed from the string \" ! \" you can use the Split overload function by passing that string as part of the string array.

string input = "\"Hello, World!\" ! \"Some other string\"";
var data = input.Split(new string[] { "\" ! \"" }, StringSplitOptions.None);
    

Comments

0

An alternative approach to the others is to use a state machine-style approach - if your start and end string delimiters were different (e.g. if they were < and > instead of ") then this would work better to support nested strings than a regex approach.

void Main()
{
    var stateMachine = new StringSplitStateMachine();

    stateMachine.Split("hi"); // hi
    stateMachine.Split("hi!lo"); // hi, lo
    stateMachine.Split("\"hi!lo\""); // "hi!lo"
    stateMachine.Split("\"hi\"!lo"); // "hi", lo
}

public class StringSplitStateMachine
{
    private readonly char _splitCharacter;
    private readonly char _stringDelimiter;
    public StringSplitStateMachine(char splitCharacter = '!', char stringDelimiter = '"')   
    {
        _splitCharacter = splitCharacter;       
        _stringDelimiter = stringDelimiter;
    }
    
    public IEnumerable<string> Split(string input)
    {
        bool insideString =false;
        var currentString = new StringBuilder();
        foreach(var character in input)
        {
            if (character == _splitCharacter && !insideString)
            {
                yield return currentString.ToString();
                currentString.Clear();
            }
            else
            {
                if (character == _stringDelimiter) 
                {
                    insideString = !insideString;
                }
                
                currentString.Append(character);
            }
        }
        
        if (currentString.Length > 0)
        {
            yield return currentString.ToString();
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.