.NET 3.5 GUI中的字符串初始化数组 [英] array of strings initialization in .NET 3.5 GUI

查看:88
本文介绍了.NET 3.5 GUI中的字符串初始化数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual Studio 2008,但我似乎无法纠正此错误..似乎很简单,但我似乎无法纠正这一错误.
IAM试图做的是使用循环将已初始化的字符串数组的值传递给未初始化的字符串数组.这是我的代码:-

Iam using visual studio 2008 and i can''t seem to rectify this error..it seems simple but i really can''t seem to rectify this.
What iam trying to do is pass the values of an initialized string array to an uninitialized string array using a loop..here''s my code:-

Document ^doc = gcnew Document("Abdul.doc");
String ^yes = doc->GetText();

array<Char>^chars = {' '};
array<String^>^splitx = yes->Split(chars);
array<String^>^ projects;
int jamrod = 0;

for (int ok = 0; ok < splitx->Length; ok++)
{
    if(splitx[ok] == "Copyright") //splitx[250]
    {
        while(splitx[ok] != "3D")
        {
            projects[jamrod] = splitx[ok];
            jamrod++;
            ok++;
        }
        break;
    }
    else
    {
        MessageBox::Show("suck on my blue suede shoe");
    }
}


如果有人能解决这个问题,我将不胜感激.
谢谢. :)


I''ll be very grateful if anyone can solve this.
Thanks. :)

推荐答案

array<string^>^ projects;
int jamrod = 0;
 
for (int ok = 0; ok < splitx->Length; ok++)
{
    if(splitx[ok] == "Copyright") //splitx[250]
    {
        while(splitx[ok] != "3D")
        {
            projects[jamrod] = splitx[ok];

您不会将项目"初始化为任何值,因此对于"jamrod"的任何值来说projects[jamrod]不存在

You don''t initialize "projects" to any value, therefore projects[jamrod] does not exist, for any value of "jamrod".


第一个问题:您没有实例化projects数组.而且看起来您这里不需要数组,但需要一个列表(因为您不知道添加到projects中的元素总数).

您应该替换:
First problem: you didn''t instanciate the projects array. And it looks like you don''t need an array here but a list (since you don''t know the total number of elements added into projects).

You should replace:
array<String^>^ projects;


作者:


by:

List<String^>^ projects = gcnew List<String^>();



然后在您的while循环中,替换:



And in you while loop, replace:

projects[jamrod] = splitx[ok];


作者:


by:

projects->Add(splitx[ok]);



第二个问题:yes字符串应该包含什么?就像您希望它至少包含"3D",对吗?否则,您的while循环将永远不会退出...除非您提供有关yes的更多详细信息,否则我将无济于事.

-----------------------------------

如果我理解得很好,您想将字符串拆分为子字符串(用'' ''分隔),并收集"Copyright""3D"之间的每个子字符串.如果我对,请使用以下代码:



Second problem: what is the yes string supposed to contain? It is like you expect it to contain at least "3D", right? Otherwise your while loop would never exit... Unless you give more details about yes, I can''t help further.

-----------------------------------

If I understood well, you want to split a string into sub-strings (separated by '' '') and collect every sub-strings between "Copyright" and "3D". If I am right, then use this code:

Document ^doc = gcnew Document("Abdul.doc");
String ^yes = doc->GetText();
array<Char>^ chars = gcnew array<Char>(1) { ' ' };
array<string^>^ splitx = yes->Split(chars);
List<string^>^ projects = gcnew List<string^>();
//this will be turned to true once we find "Copyright"
bool foundStart = false;
for (int i = 0; i < splitx->Length; i++)
{
    if (splitx[i] == "3D")
    {
        //we found the end of the list
        //so exit the loop
        break;
    }
    if (foundStart)
    {
        //"Copyright" was found before
        //so let's add this string to the list
        projects->Add(splitx[i]);
    }
    else
    {
        //"Copyright" was not found yet
        //is it this one?
        foundStart = (splitx[i] == "Copyright");
    }
}
//now projects contains every strings between "Copyright" and "3D"



否则,请再解释一次您要实现的目标.



Otherwise, please explain one more time what you want to achieve.


只需将projects更改为vector<string></string>.
Simply change projects to type vector<string></string>.
vector<string> projects;</string>


...
projects.push_back(splitx[ok]);
...



祝你好运!



Good luck!


这篇关于.NET 3.5 GUI中的字符串初始化数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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