如何动态拆分C#中的字符串值? [英] How to split the string values in C# dynamically ?

查看:78
本文介绍了如何动态拆分C#中的字符串值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的字符串值约为1750个字符。我计划一个接一个地保存500个字节,因为最多可以保存512个字节。考虑到长度限制,我可以静态保存。我想动态地做同样的事情。



我尝试过:



核心代码如下:

I am having the string value which is around 1750 characters. I am planing to save 500 bytes one by one since at a time at most 512 bytes can be saved . I am able to save statically considering the length limit. I want to do the same dynamically.

What I have tried:

Core code is as below:

try
           {
               string encLine = string.Empty;
               string encKey = string.Empty;
               using (StreamReader sr = new StreamReader(@"D:\test.txt"))
               {
                   // Read the stream to a string, and write the string to the console.
                   string line = sr.ReadToEnd();

                   int length = line.Length;
                   int nextlength = length - line.Substring(0, 500).Length;
                   for (int i = 0; i < 500; i++)
                   {
                       string test = line.Substring(0, 500);
                       SavePassword(test);
                       string test2 = line.Substring(500, nextlength);
                       SavePassword(test2);
                   }

                   //line.LastIndexOf(' ', 0, 100).ToString();
                   //Console.WriteLine(line);

               }

推荐答案

正如我上次询问时间时所说:如何在阅读时拆分文本文件C#中的文件? [ ^ ]

As I said when you asked time last time: How to split a text file on reading the file in C#?[^]
Quote:

使用 Streamreader.Read [ ^ ]你可以准确指定多少个字符一次阅读。

Use Streamreader.Read[^] and you can specify exactly how many characters to read at a time.



你也被告知:


And as you were also told:

引用:

你的文本文件受GnuPG软件保护。您必须解密才能阅读其内容。

Your text file is protected by GnuPG software. You have to decrypt it to be able to read its content.





提出相同的问题不会改变答案,即使您也想要它。 ..



Asking the same question does not change the answers, even if you want it too...


所以这里有几个问题需要解决。



首先,编码类型为如果您受限于特定字节长度,则必须事先知道字符串。我们将假设它是UTF-8,但请记住,您可能需要调整其他编码类型。



其次,你有任意字符串长度,所以你不能依赖于采取特定大小的字符串块(否则250将是完美的)。有几种方法可以实现这一点,但最简单的方法是调整你的循环:



So there's a couple of issues here that need to be addressed.

First off, the encoding type of the string is essential to know in advance if you're constrained to a specific byte length. We'll run under the assumption that it's UTF-8, but bear in mind that you may need to adjust for other encoding types.

Secondly, you have an arbitrary string length, so you cannot rely on taking specifically sized chunks of the string (otherwise 250 would be perfect). There's a few ways to skin this, but the easiest is to adapt your loop:

using(StreamReader reader = new StreamReader(@"d:\test.txt"))
{
   var stringToChunk = reader.ReadToEnd();

   //Assuming UTF-8, so 1 byte per char
   var chunkLength = 512; //may as well max it out

   for(var i = 0; i < stringToChunk.Length; i += chunkLength)
   {
      //make sure we don't run past our buffer
      if(i + chunkLength > stringToChunk.Length)
      {
         chunkLength = stringToChunk.Length - i;
      }
      
      var chunk = stringToChunk.Substring(i, chunkLength);
      // do something with chunk
   }
}


string MainAddress = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789XXXXXX";
           int TotalLenght = MainAddress.Length;
           int Lenght_1 = 0, Lenght_2 = 0, Lenght_3 = 0;
           if (TotalLenght <= 35)
           {
               Lenght_1 = TotalLenght;
           }
           else
           {
               Lenght_1 = 35;
               if (TotalLenght <= 70)
               {
                   Lenght_2 = TotalLenght-Lenght_1;

               }
               else
               {
                   Lenght_2 = 35;
                   if (TotalLenght <= 105)
                   {
                       Lenght_3 = TotalLenght-(Lenght_1+Lenght_2);

                   }
                   else
                   {
                       Lenght_3 = 35;

                   }

               }
           }


           add1.Text = MainAddress.Substring(0, Lenght_1);
           add2.Text = MainAddress.Substring(Lenght_1, Lenght_2);
           add3.Text = MainAddress.Substring(Lenght_1 + Lenght_2,Lenght_3);


这篇关于如何动态拆分C#中的字符串值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆