Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to separate a sub string from a string array where my start Char (") and my ending Char (").

My Code:
C#
int count = 0;
string[] arr1 = new string[reply.Length-20];
foreach (string item in list)
               {
                   arr1[count] = item.Split(new Char[] { '"','"' });
                   Console.WriteLine(item);
                   count = count + 1;
               }


Please help me to separate a substring from a string whit start char to end char.
Thanks in advance.

What I have tried:

arr1[count] = item.Split(new Char[] { '"','"' });
Posted
Updated 11-Jun-16 4:47am
Comments
Patrice T 10-Jun-16 12:29pm    
What is the problem.
Show some data !

Split doesn't work like that!
Possibly what you want is:
C#
int count = 0;
foreach (string item in list)
    {
    string[] arr1 = item.Split(new char[] {'"'}, StringSplitOptions.RemoveEmptyEntries);
    foreach (string s in arr1)
        {
        Console.WriteLine(s);
        count = count + 1;
        }
    }
 
Share this answer
 
The issue is not really clear. However, String.split returns an array, so you cannot store the result in arr1[count], since that is a string reference. not a string[] reference. Similarly why are you printing item in the next line, rather that some split data? You probably need to do something like:
C#
foreach (string item in list)
{
    string[] splits = item.Split(new Char[] { '"','"' });
// do something with the split subitems
}
 
Share this answer
 
It is not clear what you want to do with this code, but it can be simplified.
C#
int count = 0;
string[] arr1 = new string[reply.Length-20];
foreach (string item in list)
    {
       arr1[count] = item.Split(new Char[] { '"','"' });
       Console.WriteLine(item);
       count = count + 1;
    }

You do nothing with the variable count
 
Share this answer
 
v2
try this, if you need to get the value between two double quotes but best way to do this is using Regex [^]

C#
string some = " start \"Hello world \" end "; // start "hello world" end
          var items = some.Split('"');
          if (items.Length > 2)
          {
              string value = items[1]; // Hello world
          }
 
Share this answer
 
Comments
George Jonsson 11-Jun-16 10:36am    
Where do you use regex in your solution?
Karthik_Mahalingam 11-Jun-16 10:38am    
Hi George,
Its a suggestion to the OP, but i have just modified the code in the way he was trying to achieve the need.
George Jonsson 11-Jun-16 10:51am    
It just looks a bit odd to suggest using regex and then the code presented has nothing to do with that.
Karthik_Mahalingam 11-Jun-16 11:16am    
hmm, i understand.Lets see what the Op needs exactly.
It can be done by using a regular expression.
C#
// The expression will find any word between double quotes
Regex expr = new Regex("\"(?<word>[^"]+)\"");

string input = "There are two \"double\" quoted words in this \"string\".";
foreach (Match m in expr.Matches(input))
{
    // Using a named group makes it clear how to extract the values.
    Console.WriteLine(m.Groups["word"].Value;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900