none
string split RRS feed

  • Question

  • Hi,

    I have below use cases like:

    email: john.wills@abc.com

    email: au-account-payable@abc.com

    I do the following to extract the names from email this way:

     var email_prop = email.Remove(email.IndexOf("@"));
     var user = email_prop.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
     var name= string.Format("{0},{1}", user[1], user[0]);

    // Update the property column in sharepoint list as below

    item["Property"] = name;                                                                                                           

    I get exception for use case where email does not contain "." based on which I split as in var user

    I want to populate the Property column for each file as name from email as:

    Wills,John for email that contain "."

    OR

    au-account-payable for those where email do not contain "."

    How do I achieve this.


    Monday, February 18, 2019 12:20 AM

Answers

  • Hi, this expands on the first example by using a string that you can give the scope needed to assign to your "item property".  Again the emphasis is not whether the string contains a period "."  but more on the number of elements (Length) in the result string.

                string mystring;
    
                var email_prop = email.Remove(email.IndexOf("@"));
                var user = email_prop.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
               
                if (user.Length > 1)
                {
                    var name = string.Format("{0},{1}", user[1], user[0]);
                    mystring = name;
                }
                else
                {
                    var name = user;
                    mystring = name[0];
                }
    
                item[Property] = mystring;

    • Marked as answer by msdnpublic1234 Monday, February 18, 2019 3:27 AM
    Monday, February 18, 2019 3:09 AM

All replies

  • Hi, you don't say which exception you are getting but I think it is probably "Index Out of Range" because if there is no period to split you will not have enough elements to fill "name". To avoid that use a condition statement to validate how many elements you have. Something similar to the following

                var email_prop = email.Remove(email.IndexOf("@"));
                var user = email_prop.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
                if (user.Length > 1 && user.Length < 3)
                { var name = string.Format("{0},{1}", user[1], user[0]); item["Property"] = name;}
                else
                { var name = user;item["Property"] = name[0];}

    Monday, February 18, 2019 2:03 AM
  • Yes i get index out of range exception.Thanks Jeff

    I am writing something like this but unable to code the else portion to obtain the email_prop(entire string) in case there is no delimiter:

     var email = (string)Dts.Variables["User::Email"].Value;
                var email_prop = email.Remove(email.IndexOf("@"));
                var result = email_prop.Contains('.') ? email_prop.Split(new char[] { '.' }) : ?;
               var userString = result.Length > 1 ? string.Format("{0},{1}", result[1], result[0]) : ?

    Monday, February 18, 2019 2:15 AM
  • If i do this:

     var user = email_prop.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

    without checking if it contains delimiter,i get exception in this statement mentioning index out of range.

    Monday, February 18, 2019 2:16 AM
  • The var "name" creates 

    string[] name;

    so if the string does not contain a period then the string remains as one string  and ends up in element zero

    item["Property"] = name[0];


    Monday, February 18, 2019 2:24 AM
  • I cant do item[Property]  within if else because it is declared after that piece of code by reading context from sharepoint.

    I want to have the string array or entire string returned to result based on condition:

     var result = email_prop.Contains('.') ? email_prop.Split(new char[] { '.' }) : ?;

    Can you help with that


    Monday, February 18, 2019 2:35 AM
  • Hi, this expands on the first example by using a string that you can give the scope needed to assign to your "item property".  Again the emphasis is not whether the string contains a period "."  but more on the number of elements (Length) in the result string.

                string mystring;
    
                var email_prop = email.Remove(email.IndexOf("@"));
                var user = email_prop.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
               
                if (user.Length > 1)
                {
                    var name = string.Format("{0},{1}", user[1], user[0]);
                    mystring = name;
                }
                else
                {
                    var name = user;
                    mystring = name[0];
                }
    
                item[Property] = mystring;

    • Marked as answer by msdnpublic1234 Monday, February 18, 2019 3:27 AM
    Monday, February 18, 2019 3:09 AM
  • Jeff,

    Thanks a lot for the answer.It worked!

    Monday, February 18, 2019 3:27 AM