Symbian C++ - 描述符上的子字符串操作

发布于 2024-07-09 08:59:30 字数 325 浏览 6 评论 0原文

操作 TDesC 字符串的首选/最简单方法是什么,例如获取子字符串。

我将给你举一个我的场景的例子。

RBuf16 buf;
...
CEikLabel label;
...
label->SetTextL(buf); // (SetTextL takes a const TDesC&)

我想从 buf 中获取一个子字符串。 那么我想直接操作 RBuf16 吗?如果是的话,最好的方法是什么?

有没有办法转换为 const char* 这样我就可以使用标准 C 字符串操作。

提前致谢

What is the preferred/easiest way to manipulate TDesC strings, for example to obtain a substring.

I will give you an example of my scenario.

RBuf16 buf;
...
CEikLabel label;
...
label->SetTextL(buf); // (SetTextL takes a const TDesC&)

I want to get a substring from buf. So do I want to manipulate the RBuf16 directly and if so what is the best way?

Is there a way to convert to const char* so I can just use standard C string manipulation.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绝影如岚 2024-07-16 08:59:31

阅读descriptors.blogspot.com(加载后向下滚动)。

您可以使用 TDes::LeftTPtr、TDes::RightTPtr 或 TDes::MidTPtr,它们将为您提供一个子字符串作为 TPtr(即操作原始数据的描述符)。

如果您想创建子字符串的副本,可以使用 TDes::Copy 函数。

Read descriptors.blogspot.com (scroll down once loaded).

You can use TDes::LeftTPtr, TDes::RightTPtr or TDes::MidTPtr which will give you a substring as a TPtr (i.e. a descriptor which manipulates the original data).

You can use the TDes::Copy function if you want to create a copy of your substring.

天煞孤星 2024-07-16 08:59:31

最好与否,我无法评论,但我使用以下方法从描述符中提取子字符串:

TDes::LeftTPtr()
TDes::MidTPtr()
TDes::RightTPtr()

或者

TDesC::Left()
TDesC::Mid()
TDesC::Right()

两个集合之间的区别是前者返回一个新的可修改描述符,后者返回一个新的不可修改描述符,包含子字符串。

当您在 Symbian 平台上进行开发时,我强烈建议您遵循 Symbian 约定,而不是始终从标准 C 或 C++ 的角度来考虑 Symbian C++。 使用 Symbian 专门提供的库,而不是 Symbian 可能直接支持或可能不直接支持的标准 C/C++ 库。 由于在 Symbian 上开发的应用程序的最终目标是在移动设备上运行,其中应用程序的可靠性和稳健性最为重要,因此您应该遵循 Symbian 的偏好和建议。

Best or not, I cannot comment, but I use the following methods to extract sub-strings from descriptors:

TDes::LeftTPtr()
TDes::MidTPtr()
TDes::RightTPtr()

or

TDesC::Left()
TDesC::Mid()
TDesC::Right()

with the difference between the two sets being that the former returns a new modifiable descriptor, the latter, a new non-modifiable descriptor, containing the sub-string.

While you develop on the Symbian platform, I would suggest highly to follow the Symbian conventions, and not to think of Symbian C++ from a standard C or C++ point of view all the time. Use the libraries that Symbian has specifically made available, instead of standard C/C++ libraries that Symbian may or may not directly support. Since the ultimate goal of an application developed on the Symbian is to run on a mobile device where reliability and robustness of applications matter the most, you should stick to what Symbian prefers and suggests.

楠木可依 2024-07-16 08:59:31

如果您想要来自 RBuf16 的子字符串,那很简单 - 只需使用 TDes16::MidTPtr

您可以通过执行以下操作来转换 const char*:

TPtr8 narrowBuf;

// Create a buffer with enough space to store every character, plus one for 
// a null terminator
narrowBuf.AllocL( buf.Length() + 1);

// TPtr8::Copy accepts a TDesC16 
narrowBuf.Copy( buf );

// Append a null terminator and return a pointer to the resultant data
const char* ptr = (const char*)narrowBuf.PtrZ();

但是,您现在拥有的是原始的缩小副本数据。 如果您现在想再次设置标签,则需要在使用前将其加宽 (TDes16::Copy(const TDesC8&))。 试图让 Symbian 看起来像带有 char* 和 wchar_t* 的“正常”C++ 并没有真正起作用 - 描述符的使用在操作系统中太根深蒂固了。

If you want a substring from an RBuf16, that's straightforward - just use TDes16::MidTPtr

You can convert a const char* by doing this:

TPtr8 narrowBuf;

// Create a buffer with enough space to store every character, plus one for 
// a null terminator
narrowBuf.AllocL( buf.Length() + 1);

// TPtr8::Copy accepts a TDesC16 
narrowBuf.Copy( buf );

// Append a null terminator and return a pointer to the resultant data
const char* ptr = (const char*)narrowBuf.PtrZ();

However, what you now have a is a narrowed copy of the original data. If you want to now set the label again, you'll need to widen it (TDes16::Copy(const TDesC8&)) before use. Trying to make Symbian look like "normal" C++ with char* and wchar_t* doesn't really work - the use of descriptors is too entrenched in the OS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文