如何从分隔符之间获取字符串的一部分 [英] how to take part of a string from between delimiters
问题描述
好的,所以我试图找出如何从分隔符之间取一个字符串的一部分并为其分配另一个字符串,然后我计划将其转换为float类型。这是我到目前为止所提出的,但我以前从来没有真正在C ++中使用字符串函数(它在VB中更容易)所以我似乎遇到了string :: find()如果有人可以让它出现问题我知道我做错了什么,或者有更好的和/或更简单的方法,请告诉我:)
我使用sting :: find()错了?这个背景是错误的功能,如果是这样,我应该做什么?我不会撒谎,我发现很难阅读很多图书馆的参考页面,因为我没有学过很多技术术语,虽然我已经选了很多,自学整体,我只想偶尔得到一个解释。
Ok so I'm trying to figure out how to take a part of a string from between delimiters and assign another string to it, which I then plan to convert to a float type. This is what I came up with so far but I've never really used string functions in C++ all that much before (it's so much easier in VB) so I seem to be having problems with string::find() if someone could let me know what I'm doing wrong or if there's a much better and/or easier way to do it please let me know :)
Am I using sting::find() wrong? is it the wrong function for this context and if so what am I supposed to do? I'm not going to lie, I find it difficult to read a lot of the reference pages for libraries since I haven't been taught a lot of the technical terms though I've picked a lot up, self teaching as a whole, I just like to get an explanation every now and then.
void load(int vNum, float& toX, float& toY, float& toZ)
{
std::string line = "XSomethingY fromZ an N fstream";
std::string lCrd;
//trying to take out a specific peice of a string from between delimiter
//and one in 'from the left'
lCrd.append(line,1,line.find("Y")-1);//seems to crash the program (Y is my delimiter)
//so lCrd should be "Something" or perhaps "SomethingY" at this point
//debugger says 'line.find = ?' what does this mean/is it useful at all?
//then would convert to float and set toX = 'float(lCrd)'
lCrd = "";
lCrd.append(line,line.find("Y"),line.find("Z")-line.find("Y"));
}
推荐答案
我知道做这类事情是'性感'
I know it's 'sexy' to do this sort of thing
lCrd.append(line,1,line.find("Y")-1);//seems to crash the program (Y is my delimiter)
但问题是,如果
but the issue with it is, if
line.find("Y")-1
不评估很好或超出范围,很难发现/纠正,甚至使用调试器。
我会从'防御性编码'开始,甚至是'基本/橡皮风格'所以你可以做一些像
doesnt evaluate 'nicely' or is out of range, its hard to spot/correct, even using a debugger.
I would start by 'coding defensively', even 'basic/gumby style' so you could do somethling like
int foundpos = line.find("Y")-1;
assert (foundpos < 0)
assert (foundpos > line.length())
(这只是一个粗略的想法顺便说一句,你可以使用'if'..重点是,你有机会测试字符串位置&&处理意外的值..或者,或者开始使用'try / catch' ,和/或在你达到这一点之前采用不同的方法消毒你的琴弦。
...所以你知道我要说的是什么
(thats just a rough idea btw, you could use an 'if' .. the point is, you then get a chance to test the string positions && handle unexpected values .. either that, or start using 'try/catch', and/or have a different way of sanitising your strings before you get to this point)
... so you know what Im going to say about
lCrd.append(line,line.find("Y"),line.find("Z")-line.find("Y"));
不是你:-)
'g'
dont you :-)
'g'
int first = string.Find(_T("delimiter1"));
int last = string.Find(_T("delimiter2"));
CString temp = string.Mid(first,(last-first));
我这样工作得到两个分隔符之间的字符串。
I worked in this way to get the string between two delimiters.
这篇关于如何从分隔符之间获取字符串的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!