在字符串中查找逗号,然后将与其对应的x和y值分隔开
Finding a comma in a string then separating the x and y values that correspond with it
我目前正在为我的游戏引擎构建一个小型脚本系统的解析器。现在我正试着把一行的字符串分成几个部分,这样我就可以弄清楚信息了。例如,假设我将这一行保存到一个名为gui.ss:的脚本中
150,200
这两个整数用逗号分隔,中间没有空格。我主要想做的是找出数字的位置,以便将150保存为x值坐标整数,然后保存为y值坐标整数。任何帮助都很棒,谢谢!
使用字符串流:
std::string str;
size_t commaPosition = str.find(',');
str.replace( commaPosition, 1, " " );
stringstream ss;
ss << str;
int x, y;
ss >> x >> y;
#include <cstdlib>
#include <string>
std::string raw = "150,200";
size_t i = raw.find(',');
int x = atoi(raw.substr(0, i).c_str());
int y = atoi(raw.substr(i + 1).c_str());
std::string text = "150,200";
int x = strtol(text.c_str(), NULL, 10);
std::size_t pos = text.find(',');
int y = strtol(text.c_str() + pos + 1, NULL, 10);
我目前正在为我的游戏引擎构建一个小型脚本系统的解析器。现在我正试图把一条线分成几部分,这样我就可以算出信息
除非你真的想要,否则不要重新发明轮子。至少不是从零开始。如果您正在启动您的项目,请考虑使用现有的解析器工具。
您甚至可以使用像LUA、TCL或Python这样的嵌入式软件。它将为您的用户提供语言和现有库的功能。
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 在C++中查找文件
- 模板元程序查找相似的连续类型名称
- 如何将 txt 文件中的行分隔为组件C++
- 无法通过空白将文本文件行分隔为矢量
- 在UNIX系统中使用DIR查找文件的字节大小
- 查找最接近的大于当前数字的数字的索引
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 查找后更改类变量
- 使用正则表达式regex_search在字符串中查找字符串
- 使用gcc从静态链接的文件中查找可选符号
- 在C++中查找范围的长度
- 如何在C++中读取空格分隔的输入 当我们不知道输入的数量时
- 算法问题:查找从堆栈中弹出的所有序列
- 在Windows中查找扬声器输出的当前音量级别
- 如何在C++中使用X509证书模在令牌中查找私钥
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 在 for 循环中查找问题时遇到困难
- 查找由换行符分隔的字符串中的特定文本