BCB中如何在字符串指定位置插入新的内容

SYW# 2020-08-13 11:47:14
我知道是用Insert,但是我试了好多遍,一点反应都没有我的代码时这个样子的

if(slNC->Strings[i].SubString(1,5)=="PUN: ")
{
slNC->Strings[i].Insert(";tool note",slNC->Strings[i].SubString(slNC->Strings[i].Length()+1,6));

}

我想实现的功能是,如果检测到某一行以 “ PUN: ”开头,那就在这一行的后面插入 “;tool note”。
但是,无论我怎么试,都没有任何变化,PUN是能检测到的,就是不插入内容

Insert语法是这样的:AnsiString& Insert(const AnsiString& str, int index) 在字符串的index位置插入字符串str。
我不知道我是index 位置那弄错了,还是前面字符串str那块弄错了。

我是新学BCB的小白,希望能有高人指导一下(不怕你们笑,我已经卡这好几个小时了)
...全文
234 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
SYW# 2020-08-14
  • 打赏
  • 举报
回复
两位大哥666666666666666666666
青蛙工作室 2020-08-14
  • 打赏
  • 举报
回复
引用 5 楼 LR_LULU 的回复:
[quote=引用 3 楼 青蛙工作室 的回复:]slN->Strings[i].SubString(slNC->Strings[i].Length()+1,6) 这个返回的是个字符串,但是Insert()函数要求的第二个参数是int,根本对不上号。 改下:slNC->trings[i].Insert(";tool note", slNC->Strings[i].Length());
大哥,你好,这个方法我也试了,也没有插入进去[/quote] 那就按1楼说的方法 String str =slNC->Strings[i]; str.Insert(";tool note", str.Length()); slNC->Strings[i] = str ;
SYW# 2020-08-14
  • 打赏
  • 举报
回复
引用 3 楼 青蛙工作室 的回复:
slN->Strings[i].SubString(slNC->Strings[i].Length()+1,6) 这个返回的是个字符串,但是Insert()函数要求的第二个参数是int,根本对不上号。 改下:slNC->trings[i].Insert(";tool note", slNC->Strings[i].Length());
大哥,你好,这个方法我也试了,也没有插入进去 (PS:为什么CSDN,有时候显示有消息了,却看不到消息内容呢?)
SYW# 2020-08-14
  • 打赏
  • 举报
回复
引用 1 楼 ooolinux 的回复:
定义一个AnsiString str,str等于那个Strings[i],在str中插入,然后str赋值回去列表。


大哥,我们又见面了,大哥你看是这样吗?
if(slNC->Strings[i].SubString(1,5)=="PUN: ")
{
const AnsiString& str=slNC->Strings[i];
int index=slNC->Strings[i].Length()+1;

slNC->Strings[i].Insert(str,index);

}

这样也没有反应啊,而且,没法设置我要插入的内容

大哥,可否,给我举个实例

青蛙工作室 2020-08-14
  • 打赏
  • 举报
回复
slNC->Strings[i].SubString(slNC->Strings[i].Length()+1,6) 这个返回的是个字符串,但是Insert()函数要求的第二个参数是int,根本对不上号。 改下:slNC->Strings[i].Insert(";tool note", slNC->Strings[i].Length());
ooolinux 2020-08-14
  • 打赏
  • 举报
回复
另外一点注意,AnsiString下标从1开始,这是因为跟Delphi兼容。
ooolinux 2020-08-14
  • 打赏
  • 举报
回复
定义一个AnsiString str,str等于那个Strings[i],在str中插入,然后str赋值回去列表。
ooolinux 2020-08-14
  • 打赏
  • 举报
回复
Don't use the += AnsiString operator on properties

Examine the following code that attempts to add some exclamation points to the string that is already in a label control:

Label1->Caption += "!!!!!!!";

When you use the += operator on the Caption property, the compiler creates code that constructs a new temporary AnsiString object. The compiler then calls the GetText function to copy the contents of the label into the temporary variable. Next, the compiler constructs another AnsiString object and initializes it with the string "!!!!!". Finally, the compiler calls the += operator of the first temporary object to combine the two strings. The problem is that the compiler does not generator code to write the resulting temporary AnsiString value back into the Caption property. Instead, the temporary object is deleted because it is no longer needed.

The assembly code looks something like this:

// assembly output for the statement
Label1->Caption += "!!!!!!!";

mov ...
lea ...
call System::AnsiString::AnsiString() // create temp object
mov ...
inc ...
mov ...
mov ...
call Controls::TControl::GetText() // read Caption into temp object
lea ...
push ...
mov ...
lea ...
call System::AnsiString::AnsiString(char *) // create AnsiString for "!!!!"
inc ...
lea ...
pop ...
call System::AnsiString::operator += // combine the two strings
dec ...
lea ...
mov ...
call System::AnsiString::~AnsiString() // destroy one temp
dec ...
lea ...
mov ...
call System::AnsiString::~AnsiString() // destroy the other temp

In the assembly code above, locate the += operator call. Notice that nothing is done with the resulting string after the += operator returns. After the += operator returns, the strings are destroyed. In order for the property assignment to take affect, the resulting string should be passed to the SetText function. Because the write method for the property is not called, the TLabel object is not modified.

Through experimentation, I have found that the += operator does work on integer properties, such as the Width property of the form. However, in order to avoid confusion, it may be wise to avoid the += operator on all properties. The same can be said for the other combination operators (-= , *= , /=, ^= , and so on).
ooolinux 2020-08-14
  • 打赏
  • 举报
回复
C++Builder的属性不是成员变量,而是成员函数的封装,只支持读取和写入操作,其它操作是无法改变自身的。
类似:
class TSome
{
protected:
int a;
int b;
int s;
int GetSum(void){ return s = a + b ; }
void SetSum(int s){ this->s = s ; }
public:
//...其它代码
__property int sum={read=GetSum,write=SetSum}; //sum是属性
//...


};//end class

ooolinux 2020-08-14
  • 打赏
  • 举报
回复
引用 4 楼 LR_LULU的回复:
[quote=引用 1 楼 ooolinux 的回复:]定义一个AnsiString str,str等于那个Strings[i],在str中插入,然后str赋值回去列表。


大哥,我们又见面了,大哥你看是这样吗?
if(slNC->Strings[i].SubString(1,5)=="PUN: ")
{
const AnsiString& str=slNC->Strings[i];
int index=slNC->Strings[i].Length()+1;

slNC->Strings[i].Insert(str,index);

}

这样也没有反应啊,而且,没法设置我要插入的内容

大哥,可否,给我举个实例

[/quote] 不要用引用,用实例。

13,874

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧