1

How can this piece of code be shorten to only one line of code?

string[] result1 = content.Split('|');
string result2 = result1[1].Remove(0, 4);

Seriously sorry, but I'm at a loss right now.

5
  • 2
    Just join both lines: string result2 = content.Split('|')[1].Remove(0, 4); Commented Nov 28, 2013 at 17:21
  • 6
    Why do you want to only use a single line of code? (And do you still need both variables?) More context is definitely required here. Commented Nov 28, 2013 at 17:22
  • 1
    Jamming multiple logical steps into one line of code makes the code harder to read, debug and maintain. It doesn't run any faster and it is the mark of an amateur Commented Nov 28, 2013 at 17:26
  • 1
    And to what Steve said, you should add that, in this specific case, the reliability is potentially lower: string[] result1 = content.Split('|'); string result2 = ""; if(result1.Length > 1) result2 = result1[1].Remove(0, 4); Works always. The summarised version you are asking for crashes if | is not present in content. Reducing the length of the code can be good in some contexts, but not always. Commented Nov 28, 2013 at 17:31
  • i know, but i got stuck in my code, since I'm sitting here programming for hours. Could not remember how to solve it. Joining both lines together is propably what i needed and other information is not necessary. So, I assume combining those two lines of code will not inconvenience myself :-P Commented Nov 28, 2013 at 17:35

5 Answers 5

3

If you need to complete array back with first item modified.

string[] result = content.Split('|').Select((r, i) => 
                        new { Value = i == 0 ? r.Remove(0, 4) : r, Index = i })
                        .Select(r => r.Value)
                        .ToArray();
Sign up to request clarification or add additional context in comments.

2 Comments

I know, it's not actually what I asked for, but I like this expression better. This keeps my array original, with small modifications. I adjusted accordingly the value from 0 to 1, because it is the second placeholder i need to modify. thank you
@MrMAG, Somehow I read var result instead of string result and I assumed that you needed a modified array. That is why I posted it. But consider Jon Skeet comment, having it on a single line doesn't improve readability
3
string result2 = content.Split('|')[1].Remove(0, 4);

Comments

3

Try this by joining both the lines like this:-

string result2 = content.Split('|')[1].Remove(0, 4);

Although as Jon Skeet mentioned in the comments it looks very logical question as to why you want to do this? As it is highly recommended to write a readable code.

Comments

3

a different answer try it

 public static class MyExtensions
   {
     public static string SplitRemove(this String str,int arrayIndex,int charToRemove)
     {
        return str.Split(new char[] { '|' },
                         StringSplitOptions.RemoveEmptyEntries)   [arrayIndex].Remove(0,charToRemove);
    }
}   

1 Comment

ok you write it once and whenever you call it you have one line of code
1
string result2 = content.Split('|')[1].Remove(0, 4);

1 Comment

This is a question about C#.

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.