0

I want to know how to split the string into different lengths or chunks. E.g. I want the name to be split into 0 to 19 with 0 the starting position and 19 the ending position. Any ideas on how I could do this?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace employeefinal
{
    class Program
    {
        static void Main(string[] args)
        {
            employee i = new employee("Tom");
            Console.WriteLine(i.getString());
            Console.ReadLine();
        }

        public class employee
        {
            string employeename = "Name: John Smith, L, U, 012, 2, 7, 2, 4";

            public employee(string name)
            {
                this.employeename = name;
            }

            public string getString()
            {
                employeename.Substring(0, 19).Trim();
                return employeename;
            }
        }
    }
}
6
  • 1
    that's what you already did in your getString, but C# String is immutable, so you'll need to return the SubString result Commented Nov 9, 2015 at 21:19
  • not working though as it is not retrieving anything in my main when starting the program Commented Nov 9, 2015 at 21:20
  • 1
    as both of us said in the comments, return employeename.Substring(0, 19).Trim(); Commented Nov 9, 2015 at 21:23
  • Mind me asking why you want to do this? There is probably a much better way to solve your problem. Commented Nov 9, 2015 at 21:25
  • so what does the letter U represent in that string..? Commented Nov 9, 2015 at 21:27

3 Answers 3

1

As i understand, you want to split on comma separator, you have two options:

Option 01:

  public string getString()
  {
      return employeename.Substring(0, employeename.IndexOf(',').Trim();
  }

Option 02:

    public string getString()
    {
        return employeename.Split(',').FirstOrDefault();
    }

Happy to Help you!

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

3 Comments

that's not what he asked, but if that what he meant, then KUDOS :)
If i not answer like this now, a new ask will be created, ^^ Thank you!
return employeename.Split(',').FirstOrDefault().Replace("Name:", string.Empty); even better no need for OP to have Name: since the code already tells us that it's pulling back first name based on the first , it finds
1

You probably want to do as below, since strings are immutable in C#. When you do like you had, the "effect" is lost because you are not assigning that result to anything.

return employeename.Substring(0, 19).Trim();

Also note you lose original string once you assign "Tom" to it, through the constructor.

PS. When you pass start position and length which go out of bounds of the string instance you will get an exception.

11 Comments

Done this already as you can see in cod but not working though as it is not retrieving anything in my main when starting the program.
@ramteen1993: What you have done is different, try this.
@ramteen1993 just return the employeename.Substring(0, 19).Trim(); don't return the "emploteename"
@ArielB what's emploteename
@MethodMan it's the data member he wants to split (look at his post)
|
0

following to what Giorgi said, change the implementation:

      public string getString()
        {
            if (employeename.Length > 19)
            {
                 return employeename.Substring(0, 19).Trim();
            }
            else
            {
                  //return error OR
                  return employeename;
            }

        }

also, your string is too short when assigning "tom" to it, you must make sure that you have atleast 19 characters.. i don't have VS in front of me, but basiclly the above implementation is better

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.