C# 如何拆分文本文件中的字符串(IP地址)?

C# 如何拆分文本文件中的字符串(IP地址)?,c#,vb.net,C#,Vb.net,我有一个ip地址内容如下的文本文件 10.1.11.88 10.1.11.52 10.1.11.35 10.1.11.95 10.1.11.127 10.1.11.91 如何从文件中拆分ip地址 var ipAddresses = File.ReadAllLines(@"C:\path.txt"); 这将为文本文件的每一行创建一个带有单独字符串的数组。我还将使用ipaddress.tryparse验证读取的字符串- 您可以使用ips[i].GetAddressBytes()拆分地址。如果您

我有一个ip地址内容如下的文本文件

10.1.11.88
10.1.11.52
10.1.11.35
10.1.11.95
10.1.11.127
10.1.11.91
如何从文件中拆分ip地址

var ipAddresses = File.ReadAllLines(@"C:\path.txt");

这将为文本文件的每一行创建一个带有单独字符串的数组。

我还将使用ipaddress.tryparse验证读取的字符串-


您可以使用
ips[i].GetAddressBytes()
拆分地址。

如果您希望将单个IP地址拆分为四(4)个组件,请使用
string.split(char[])
,这将为您提供一个包含每个部分的
字符串[]

例如:

string[] addressSplit = "10.1.11.88".Split('.');
// gives { "10", "1", "11", "88" }

这应该对你有用。这是鱼:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            System.IO.StreamReader myFileStream;
            string strFileLine;
            String[] arrAddressString;


            myFileStream = new System.IO.StreamReader("c:\\myTextFile.txt");
            // where "c:\\myTextFile.txt" is the file path and file name.

            while ((strFileLine = myFileStream.ReadLine()) != null)
            {

                arrAddressString = strFileLine.Split('.');

            /*
               Now we have a 0-based string arracy
               p.q.r.s: such that arrAddressString[0] = p, arrAddressString[1] = q,
               arrAddressString[2] = r, arrAddressString[3] = s
            */

            /* here you do whatever you want with the values in the array. */

                // Here, i'm just outputting the elements...

                for (int i = 0; i < arrAddressString.Length; i++)
                {
                    System.Console.WriteLine(arrAddressString[i]);
                }

                System.Console.ReadKey();
             }
        }
    }
}
使用系统;
使用System.IO;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
System.IO.StreamReader myFileStream;
字符串字符串字符串;
字符串[]ArraAddressString;
myFileStream=new System.IO.StreamReader(“c:\\myTextFile.txt”);
//其中“c:\\myTextFile.txt”是文件路径和文件名。
而((strFileLine=myFileStream.ReadLine())!=null)
{
ArraAddressString=strFileLine.Split('.');
/*
现在我们有了一个基于0的字符串数组
p、 q.r.s:这样ArraAddressString[0]=p,ArraAddressString[1]=q,
ArraAddressString[2]=r,ArraAddressString[3]=s
*/
/*在这里,您可以对数组中的值执行任何操作*/
//在这里,我只是输出元素。。。
for(int i=0;i
文件。阅读行还不够吗?你的问题不清楚你想要什么。需要帮助读取文件中的行,或者分离IP地址本身的组件吗?我想拆分IP并在另一个函数中使用。这将为您提供一个字符串数组。iPAddresss[1]是您的第一个地址等@MeysamSavameri:请更具体一点。当你说“分割IP”时,我理解这意味着你想要一个阵列,其中每个IP地址都在它自己的线路上。如果这不是您想要的,那么提供一个示例,说明您希望从示例文本文件获得的输出如何?
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            System.IO.StreamReader myFileStream;
            string strFileLine;
            String[] arrAddressString;


            myFileStream = new System.IO.StreamReader("c:\\myTextFile.txt");
            // where "c:\\myTextFile.txt" is the file path and file name.

            while ((strFileLine = myFileStream.ReadLine()) != null)
            {

                arrAddressString = strFileLine.Split('.');

            /*
               Now we have a 0-based string arracy
               p.q.r.s: such that arrAddressString[0] = p, arrAddressString[1] = q,
               arrAddressString[2] = r, arrAddressString[3] = s
            */

            /* here you do whatever you want with the values in the array. */

                // Here, i'm just outputting the elements...

                for (int i = 0; i < arrAddressString.Length; i++)
                {
                    System.Console.WriteLine(arrAddressString[i]);
                }

                System.Console.ReadKey();
             }
        }
    }
}