none
put this into array or collection RRS feed

  • Question

  • Hi,

    I have string like paragraph. I want to put each sentence of line into each field , ex: job title : JOb description : salary : Department.


    currently the string is like this - /*

    Business Admin

    This job is for staff with high experience.

    20,0000 */

    Please anyone know to detect the next line and put them into collection.




    abs

    Monday, January 7, 2019 10:00 AM

All replies

  • Please provide an example as it exists in the string currently and where did the string comes from? More details are best, otherwise we need to ask more question while if you provide all details less questions or no questions from you will be needed.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, January 7, 2019 12:36 PM
    Moderator

  • to detect the end of line is check if your caracter is "\n"

    Monday, January 7, 2019 2:46 PM
  • If you need to break apart a string then take a look at String.Split. That is what it is designed to do. The challenge with newlines is there are a couple of ways to do them. So you might need to handle CR, LF and CRLF. Windows tends to use CRLF so splitting on Environment.NewLine should be sufficient.

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, January 7, 2019 4:37 PM
    Moderator
  • Hi,

    what do you mean? The next line? 

    Your string:

    Business Admin 

    This job is for staff with high experience.

    20,0000

    xxx

    Next Job

    Very gut job!

    10,-

    xxx

    ...

    So how others said, use split, and you will have 4 times the count of jobs as string count. Now you could use a for loop to fill some DTOs.

    Eg. you got 3 jobs, and 9 strings, so you can use n=index of current job(starting with 0) [n+0]=job title, [n+1]=description, [n+2]=department. n++; After every cycle, you can add the new object to a list...

    Greetings, Chris


    • Edited by DerChris88 Tuesday, January 8, 2019 12:02 AM
    Monday, January 7, 2019 5:47 PM
  • Here is an example how you could do it:

        public class Program
        {
            public class JobDto
            {
                public string JobTitle { get; set; }
                public string Description { get; set; }
                public string Salary { get; set; }
                public string Departement { get; set; }
            }
    
            static void Main(string[] args)
            {
                string input = @"Business Admin 
    
    This job is for staff with high experience.
    
    20,0000
    
    Office
    
    Janitor 
    
    This job is for staff with high experience.
    
    21,0000
    
    Cellar";
                char[] stringSeparators = new char[] { '\r','\n' };
                var array = input.Split(stringSeparators).Where(x => x.Trim() != "").ToArray();
                var list = new List<JobDto>();
                for (int i = 0 ; i < array.Length / 4 ; i++)
                {
                    list.Add(new JobDto {
                        JobTitle = array[i * 4],
                        Description = array[i * 4 + 1],
                        Salary = array[i * 4 + 2],
                        Departement = array[i * 4 + 3]
                    });
                }
            }
    
     
        }

    Greetings, Chris

    Monday, January 7, 2019 11:27 PM
  •         private static List<JobDto> v2(string input)
            {
                char[] stringSeparators = new char[] { '\r', '\n' };
                var array = input.Split(stringSeparators).Where(x => x.Trim() != "").ToArray();
                var list = new List<JobDto>();
                for (int i = 0; i < array.Length; i+=4)
                {
                    list.Add(new JobDto
                    {
                        JobTitle = array[i],
                        Description = array[i + 1],
                        Salary = array[i + 2],
                        Departement = array[i + 3]
                    });
                }
                return list;
            }

    Monday, January 7, 2019 11:48 PM
  • Hi Abhinandan Abz,

    Is there any update? do you try the method that DerChris88 provided, if the issue still exists, please feel free let us know.
    Best regards,

    Jack

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 16, 2019 3:12 AM
    Moderator