Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trim IP Address to get the first 3 parts of it?

Tags:

string

c#

trim

I need to trim a given IP Address to get the first 3 parts of it

Example:

"192.168.1.20" ➨ "192.168.1."
"29.6.60.241" ➨ "29.6.60."

like image 508
Murhaf Sousli Avatar asked Mar 16 '12 13:03

Murhaf Sousli


People also ask

How do you split an IP address?

A subnet mask is used to divide an IP address into two parts. One part identifies the host (computer), the other part identifies the network to which it belongs.

What are the 3 IP addresses?

An internet protocol (IP) address allows computers to send and receive information. There are four types of IP addresses: public, private, static, and dynamic.

What are the parts in IP address?

An IP address has two parts: the network ID, comprising the first three numbers of the address, and a host ID, the fourth number in the address.

What is starting IP address and ending IP address?

Start IP: Type an IP address to serve as the start of the IP range that DHCP will use to assign IP addresses to all LAN devices connected to the router. End IP: Type an IP address to serve as the end of the IP range that DHCP will use to assign IP addresses to all LAN devices connected to the router.


2 Answers

String result = input.Substring(0, input.LastIndexOf("."));

like image 150
Max Keller Avatar answered Jan 04 '23 02:01

Max Keller


Using String.LastIndexOf(), it should be easy.

EDIT
Using that method you can locate the last '.'. Then you need a substring up to and (apparently) incuding that '.'. Something like:

string shortened = longIP.Substring(0,longIP.LastIndexOf(".")+1);
like image 22
Hans Kesting Avatar answered Jan 04 '23 02:01

Hans Kesting