6

I have a program, in which you can input a string. But I want text between quotes " " to be removed.

Example:

in: Today is a very "nice" and hot day.

out: Today is a very "" and hot day.

        Console.WriteLine("Enter text: ");
        text = Console.ReadLine();

        int letter;
        string s = null;
        string s2 = null;
        for (s = 0; s < text.Length; letter++)
        {
            if (text[letter] != '"')
            {
                s = s + text[letter];
            }
            else if (text[letter] == '"')
            {

                s2 = s2 + letter;
                letter++;
                (text[letter] != '"')
                {
                    s2 = s2 + letter;
                    letter++;
                }
            }
        }

I don't know how to write the string without text between quotes to the console. I am not allowed to use a complex method like regex.

Community
  • 1
  • 1
Jolek111
  • 89
  • 1
  • 7
  • 1
    Can you have multiple quoted strings? If so, do you want to remove them all? – Matthew Watson Jan 21 '16 at 10:58
  • 4
    If this is homework, it's best to answer with general pointers rather than supplying the answer verbatim... – Matthew Watson Jan 21 '16 at 11:07
  • I would assume this is homework so if you really want to do it in a basic way , have a variable `bool inside = false;` and change it when you encounter any quote mark, when it is false write from the string, when not don't. – Mihai-Daniel Virna Jan 21 '16 at 11:12
  • This code of yours does what is expected of it, and as I assume it is a homework assignment, I would think this will have helped you with both looping and conditions. No need to change it really. – jvanrhyn Jan 21 '16 at 11:18
  • 1
    Today is a very "nice" and "hot" "day. what's the result? – LuckyMN Z Jan 21 '16 at 11:26

7 Answers7

7

This should do the trick. It checks every character in the string for quotes. If it finds quotes then sets a quotesOpened flag as true, so it will ignore any subsequent character.

When it encounters another quotes, it sets the flag to false, so it will resume copying the characters.

Console.WriteLine("Enter text: ");
text = Console.ReadLine();

int letterIndex;
string s2 = "";
bool quotesOpened = false;
for (letterIndex= 0; letterIndex< text.Length; letterIndex++)
{
    if (text[letterIndex] == '"')
    {
        quotesOpened = !quotesOpened;

        s2 = s2 + text[letterIndex];
    }
    else 
    {
        if (!quotesOpened)
            s2 = s2 + text[letterIndex];
    }
}

Hope this helps!

GigiSan
  • 1,170
  • 2
  • 19
  • 30
5

A take without regular expressions, which I like better, but okay:

string input = "abc\"def\"ghi";
string output = input;

int firstQuoteIndex = input.IndexOf("\"");

if (firstQuoteIndex >= 0)
{
    int secondQuoteIndex = input.IndexOf("\"", firstQuoteIndex + 1);

    if (secondQuoteIndex >= 0)
    {
        output = input.Substring(0, firstQuoteIndex + 1) + input.Substring(secondQuoteIndex);
    }
}

Console.WriteLine(output);

What it does:

  • It searches for the first occurrence of "
  • Then it searches for the second occurrence of "
  • Then it takes the first part, including the first " and the second part, including the second "

You could improve this yourself by searching until the end of the string and replace all occurrences. You have to remember the new 'first index' you have to search on.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
3

First we need to split string and then remove odd items:

    private static String Remove(String s)
    {
        var rs = s.Split(new[] { '"' }).ToList();
        return String.Join("\"\"", rs.Where(_ => rs.IndexOf(_) % 2 == 0));
    }

    static void Main(string[] args)
    {
        var test = Remove("hello\"world\"\"yeah\" test \"fhfh\"");
        return;
    }
Andrey Burykin
  • 700
  • 11
  • 23
  • This solution is neat, but it also removes the quotes that must be left in the output. `out: Today is a very "" and hot day. ` – jvanrhyn Jan 21 '16 at 11:11
  • Looks like your previous solution was good. Except a few drawbacks. Why current solution is better than `String.Join("", input.Split('"').Select((part, index) => index % 2 != 0 ? "\"\"" : part))`? – Kote Jan 21 '16 at 11:57
  • Because it needs to add first index of quote: private static String Remove(String s) { var index = s.IndexOf('"'); var rs = s.Split(new[] { '"' }).ToList(); return String.Join("\"", rs.Where(_ => (rs.IndexOf(_)+index+1) % 2 == 0)); } – Andrey Burykin Jan 21 '16 at 12:01
3
string text = @" Today is a very ""nice"" and hot day. Second sentense with ""text"" test";
Regex r = new Regex("\"([^\"]*)\"");
var a = r.Replace(text,string.Empty);

Please try this.

Mahesh Malpani
  • 1,782
  • 16
  • 27
1

This would be a possible solution:

String cmd = "This is a \"Test\".";
// This is a "".
String newCmd = cmd.Split('\"')[0] + "\"\"" + cmd.Split('\"')[2];
Console.WriteLine(newCmd);
Console.Read();

You simply split the text at " and then add both parts together and add the old ". Not a very nice solution, but it works anyway.

€dit:

cmd[0] = "This is a "
cmd[1] = "Test"
cmd[2] = "."
kaliba
  • 230
  • 2
  • 12
1

You can do it like this:

Console.WriteLine("Enter text: ");
var text = Console.ReadLine();

var skipping = false;

var result = string.Empty;

foreach (var c in text)
{
    if (!skipping || c == '"') result += c;
    if (c == '"') skipping = !skipping;
}

Console.WriteLine(result);

Console.ReadLine();

The result string is created by adding characters from the original string as long we are not between quotes (using the skipping variable).

Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
  • 1
    It can't be news to you that code blocks are quite often just copied in to a solution to see if the correct results are seen, which just leads to further questions down the line when OP's have to then extend functionality which could be avoided by a simple explanation that I bet took you less than 30 seconds to write. A comment is just that, a comment, do what you like with it. I was just suggesting if you want to play FGITW you may as well do it right. – Sayse Jan 21 '16 at 11:14
1

Take all indexes of quotes remove the text between quotes using substring.

    static void Main(string[] args)
    {
        string text = @" Today is a very ""nice"" and hot day. Second sentense with ""text"" test";

        var foundIndexes = new List<int>();
        foundIndexes.Add(0);
        for (int i = 0; i < text.Length; i++)
        {
            if (text[i] == '"') 
                foundIndexes.Add(i);

        }

        string result = "";

        for(int i =0; i<foundIndexes.Count; i+=2)
        {
            int length = 0;

            if(i == foundIndexes.Count - 1)
            {
                length = text.Length - foundIndexes[i];
            }
            else
            {
                length = foundIndexes[i + 1] - foundIndexes[i]+1;
            }
            result += text.Substring(foundIndexes[i], length);

        }

        Console.WriteLine(result);
        Console.ReadKey();
    }

Output: Today is a very "" and hot day. Second sentense with "" test";

Here dotNetFiddle

mybirthname
  • 17,949
  • 3
  • 31
  • 55