avoiding to use temp strings with .c_str command? - substr

I'm wondering if there is a way to avoid using a temporary string in this case:
tempString = inputString.substr(commaLast,commaCurrent-1);
yPos = strtod(tempString.c_str(), NULL);
There is no way to use the substr command and returning a c_str without first storing the substring in a temp string (supposing I dont want to modify the original string).

Have you tried the following?
yPos = strtod(inputString.substr(commaLast,commaCurrent-1).c_str(), NULL);

Related

String indexing in MATLAB: single vs. double quote

I have a matrix of strings such as the following:
readFiles = [
"11221", "09";
"11222", "13";
"12821", "06";
"13521", "02";
"13522", "13";
"13711", "05";
"13921", "01";
"14521", ".001";
"15712", ".003"
];
These are used to access to some folders and files in an automatic way. Then what I want to do is the following (with ii being some integer):
FileName = strcat('../../Datasets/hc-1/d',readFiles(ii,1),'/d',...
readFiles(ii,1),readFiles(ii,2),'.dat');
data(ii,:) = LoadBinary(FileName, 6);
The string FileName is then generated using double quotes (I'm not sure why). So its value is:
FileName =
"../../Datasets/hc-1/d13921/d1392101.dat"
The function LoadBinary() returns an error when trying to perform the following operation:
lastdot = strfind(FileName,'.');
FileBase = FileName(1:lastdot(end)-1); % This line
However, if I create the string FileName manually using single quotes, the function works okay.
In a nutshell, if I try to index a string (FileName(1:lastdot(end)-1)) that is created with the lines above (leading to FileName = "../../Datasets/hc-1/d13921/d1392101.dat"), MATLAB returns an error. If I create it manually with single quotes (FileName = '../../Datasets/hc-1/d13921/d1392101.dat'), the function works right.
Why does this happen? Is there a way to fix it (i.e. convert the double-quoted string into a single-quoted one)?
Double quotes are String array, while Single one are Char array. You can convert your string array to a char one using the function char.
So you'd write :
CharFileName = char(FileName)
And it should resolve your issue.

Remove part of string (regular expressions)

I am a beginner in programming. I have a string for example "test:1" and "test:2". And I want to remove ":1" and ":2" (including :). How can I do it using regular expression?
Hi andrew it's pretty easy. Think of a string as if it is an array of chars (letters) cause it actually IS. If the part of the string you want to delete is allways at the end of the string and allways the same length it goes like this:
var exampleString = 'test:1';
exampleString.length -= 2;
Thats it you just deleted the last two values(letters) of the string(charArray)
If you cant be shure it's allways at the end or the amount of chars to delete you'd to use the version of szymon
There are at least a few ways to do it with Groovy. If you want to stick to regular expression, you can apply expression ^([^:]+) (which means all characters from the beginning of the string until reaching :) to a StringGroovyMethods.find(regexp) method, e.g.
def str = "test:1".find(/^([^:]+)/)
assert str == 'test'
Alternatively you can use good old String.split(String delimiter) method:
def str = "test:1".split(':')[0]
assert str == 'test'

how to separate a string char by char and add a symbol after in c#

Any idea how i can separate a string with character and numbers, for example
12345ABC678 to make it look like this
1|2|3|4|5|A|B|C|6|7|8??
Or if this is not possile, how can i take this string a put every character or nr of it in a different textBox like this?
You can use String.Join and String.ToCharArray:
string input = "12345ABC678";
string result = String.Join("|", input.ToCharArray());
Instead of ToCharArray(creates a new array) you could also cast the string to IEnumerable<char> to force it to use the right overload of String.Join:
string result = String.Join("|", (IEnumerable<char>)input);
use
String aString = "AaBbCcDd";
var chars = aString.ToCharArray();
Then you can loop over the array (chars)

Parsing a string to find next delimiter - Processing

So the idea here is that I'm taking a .csv into a string and each value needs to be stored into a variable. I am unsure how to properly parse a string to do this.
My idea is a function that looks like
final char delim = ',';
int nextItem(String data, int startFrom) {
if (data.charAt(startFrom) != delim) {
return data.charAt(startFrom)
} else {
return nextItem(data, startFrom + 1);
}
}
so if I passed it something like
nextItem("45,621,9", 0);
it would return 45
and if I passed it
nextItem("45,621,9", 3);
it would return 621
I'm not sure if I have that setup properly to be recursive, but I could also use a For loop I suppose, only real stipulation is I can't use the Substring method.
Please don't use recursion for a matter that can be easily done iteratively. Recursion is expensive in terms of stack and calling frames: A very long string could produce a StackOverflowError.
I suggest you take a look to standard method indexOf of java.lang.String:
A good alternative is Regular Expressions.
You can seperate the words considering comma ',' as delimeter
Code
String[] nextItem(String data) {
String[] words=data.split(",");
return words;
}
This will return an array of strings that is the words in your input string. Then you can use the array in anyway you need.
Hope it helps ;)
Processing comes with a split() function that does exactly what you're describing.
From the reference:
String men = "Chernenko,Andropov,Brezhnev";
String[] list = split(men, ',');
// list[0] is now "Chernenko", list[1] is "Andropov"...
Behind the scenes it's using the String#split() function like H. Sodi's answer, but you should just use this function instead of defining your own.

Is there a way I can remove the range of characters form CString?

As we can use string.erase function in std::string to remove certain range of characters is there any function I can use to remove the characters.
For eg:
std::string string = "This is a test".
I could use
string.erase(2,(string.length()));
Is there any similar method in CString?
Thanks.
There are CString::Left() and CString::Right() functions. The alternative to your code would be:
CString result = string.Left(2);
Or if you would like to remove characters in the middle, you can use something like this:
CString result = string.Left(removeStart) + string.Right(removeEnd);
Yes of course. Take a look at CString Extraction Methods
Your code snippet can be achieved by
CString s = "This is a test".
s = s.Left(2);
ASSERT(s == "Th");
To remove a range of characters so to keep the first m and last n characters, you can do:
s = s.Left(m) + s.Right(n);
CString.Delete perhaps?
Documentation: https://learn.microsoft.com/en-us/cpp/atl-mfc-shared/reference/cstringt-class#delete

Resources