1

Hi guys i was trying a lot to retrieve some other strings from one main string.

string src = "A~B~C~D";

How can i retrieve separately the letters? Like:

string a = "A";
string b = "B";
string c = "C";
string d = "D";
2
  • 1
    begin with string.Split and then trim chars you want to get rid of. Commented Sep 26, 2013 at 14:36
  • Anything special about ~ and what have you tried Commented Sep 26, 2013 at 14:36

8 Answers 8

16

you could use Split(char c) that will give you back an array of sub strings seperated by the ~ symbol.

string src = "A~B~C~D";

string [] splits = src.Split('~');

obviously though, unless you know the length of your string/words in advance you won't be able to arbitrarily put them in their own variables. but if you knew it was always 4 words, you could then do

string a = splits[0];
string b = splits[1];
string c = splits[2];
string d = splits[3];
Sign up to request clarification or add additional context in comments.

Comments

6

Try this one. It will split your string with all non-alphanumeric characters.

string s = "A~B~C~D";
string[] strings = Regex.Split(s, @"\W|_");

Comments

5

Please try this

string src = "A~B~C~D"
//
// Split string on spaces.
// ... This will separate all the words.
//
string[] words = src .Split('~');
foreach (string word in words)
{
    Console.WriteLine(word);
}

Comments

3

You can do:

string src = "A~B~C~D";

string[] strArray = src.Split('~');

string a = strArray[0];   
string b = strArray[1];   
string c = strArray[2];
string d = strArray[3];

Comments

3
string src = "A~B~C~D";
string[] letters = src.Split('~');

foreach (string s in letters)
{
    //loop through array here...
}

1 Comment

Oops - need to type faster I think. 5th answer posted by the time I pressed submit :)
2

Consider...

string src = "A~B~C~D";
string[] srcList = src.Split(new char[] { '~' });
string a = srcList[0];
string b = srcList[1];
string c = srcList[2];
string d = srcList[3];

Comments

1
string words[]=Regex.Matches(input,@"\w+") 
                    .Cast<Match>()
                    .Select(x=>x.Value)
                    .ToArray();

\w matches a single word i.e A-Z or a-z or _ or a digit

+is a quantifier which matches preceding char 1 to many times

2 Comments

@JeroenvanLangen he has edited his question ...his initial question was to select all the words from string.
omg, thank you guys :O So many answers in such a short time. WOW !! I've never seen something like this :))
1

I like to do it this way:

        List<char> c_list = new List<char>();
        string src = "A~B~C~D";
        char [] c = src.ToCharArray();
        foreach (char cc in c)
        {
            if (cc != '~')
                c_list.Add(cc);
        }

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.