snippetcsharpModerate
Processing a list to build an ip in a string format
Viewed 0 times
formatbuildlistprocessingstring
Problem
The point of this code is to build an ip from a list of type
The code works and outputs an ip as expected, but the formatting I give it leaves to be desired, having to delete the last element of the string does not look like a reliable solution or at least, I don't feel like it is.
The code above would output, before returning, the following string:
"192.168.1.1."
And after removing the last character it will look like this:
"192.168.1.1"
ushort.var ip = new StringBuilder();
List ipList = new List(4) {192, 168, 1, 1};
ipList.ToList().ForEach(x => ip.Append(x + "."));
return ip.Remove(ip.Length - 1, 1).ToString();The code works and outputs an ip as expected, but the formatting I give it leaves to be desired, having to delete the last element of the string does not look like a reliable solution or at least, I don't feel like it is.
The code above would output, before returning, the following string:
"192.168.1.1."
And after removing the last character it will look like this:
"192.168.1.1"
Solution
There is a
btw, you don't need to call
string.Join() method which would exactly do what you want like so string ip = string.Join(".", ipList);btw, you don't need to call
ToList() on a List.Code Snippets
string ip = string.Join(".", ipList);Context
StackExchange Code Review Q#112009, answer score: 12
Revisions (0)
No revisions yet.