1

I have a string array for Abc\r\n123PQR\r\n456 which i got from a container and I want only "ABC" and "PQR"using webdriver. How to get it ?

I have used Split("\r\n") but its only returning 2 strings "ABC"and 123PQR\r\n456

Any suggestions? Language is C# using webdriver.

4
  • 2
    Can you show your actual code? Split should have given you 3 strings based on the example you've given. Also don't spam unrelated tags (I don't see how this pertains to java) Commented Jul 26, 2016 at 18:27
  • Added an edit to get rid of useless tags. This is just a question about c# strings and split. Commented Jul 26, 2016 at 18:30
  • That also does not look like a string array to me. Commented Jul 26, 2016 at 18:30
  • What does this have to do with java, selenium and webdriver? Commented Jul 26, 2016 at 18:57

2 Answers 2

1

To get words in sequence try this

string sequence = "Abc\r\n123PQR\r\n456";

string[] itemsArray = sequence.Split(new char[] { '\r', '\n' },
    StringSplitOptions.RemoveEmptyEntries);

List<string> itemsList = new List<string>(itemsArray);

List<string> itemsListFind = itemsList.FindAll(
    delegate(string item) 
    { 
        return 
            item.ToUpper().Contains("ABC") ||
            item.ToUpper().Contains("PQR"); 
    });

string[] result = itemsListFind.ToArray();

The result is:

{string[2]}
    [0]: "Abc"
    [1]: "123PQR"

Is it?

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

1 Comment

You can simplify your split by doing: Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries) since the OP's string does not contain \r or \n independent of each other. This is assuming though they are truly looking for the newline.
0

Edit, according to test cases proposed:

        Regex reg = new Regex(@"([A-Za-z].*)", RegexOptions.Singleline);
        // Split the string on line breaks, removing parts consisted of only numbers
        // ... The return value from Split is a string array.
        string[] lines = Regex.Split(Regex.Replace(value, @"(?<=\n)(.[\d]*)(?=\r)|(?<=\n)(.[\d]*)(?=$)|(?<=^)(.[\d]*)(?=\r)", string.Empty), "\r\n");
        //Removing empty spaces with Linq
        lines = lines.Where(x => !string.IsNullOrEmpty(x)).ToArray(); 
        for (int i = 0; i < lines.Count(); i++) 
        {
            Match m = reg.Match(lines[i]);
            if (m.Success)
            {
                lines[i] = m.Value;
            }
            Console.WriteLine(lines[i]);
        }
    }

5 Comments

I have used the below code and it works fine, string[] lines = Regex.Split(Regex.Replace(value, @"[\d]", string.Empty), "\r\n"); But the real problem started when I have some string value having numbers in name like Actual name: 4547gaeey\r\n12345567mona2p\r\n123456lisa123\r\n8764438 I want to get output like : Expected name: 2947gaeey,mona2p,lisa123
hm, I thought that you only wanted the names, like "gaeey", "mona" and "lisa" in this case. I'm gonna update the code
actually why do you have "2947gaaey" as expected name? please give me the correct requirement so that I can build a regex for you
I have edited it @Monade . Take a look at it. Now the output for the last test case that you showed here is: "gaaey" , "mona2p" ,"lisa123"
Thanks. Actually i am creating an automated script, and I have to fetch some specific string from a container from UI. So for first time I have used ur earlier script of regex , and it had solved the issue. Then I got to know that the string could have digits too. For e.g. Sam2 , 265-paul etc. So in this case my previous script will fail.

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.