How to concatenate QString and int - c++

I am trying to set the text that appears on a tab to something like this
~Untitled(n)
Where "n" is the index of the tab. I am having trouble concatenating the string and integer. This is what I have tried.
armaTab->addTab(new QWidget, "~Untitled (" + QString(armaTab->currentIndex() + 1) + ")");
With that, i end up getting something that looks like this:
~Untitled([])
What is the proper way to concatenate the string and integer to produce the desired result?

"~Untitled (" + QString::number(armaTab->currentIndex() + 1) + ")"
= OR =
QString("~Untitled(%1)").arg(armaTab->currentIndex() + 1)

Try using QString::number(n). This will convert the integer to a QString which you can concatenate to your original string.

QString offers the arg function:
QString("~Untitled %1").arg(armaTab->currentIndex() + 1)

This question already have few good answers. I'm adding another alternate way to concatenate string and integer using stringstream
stringstream ss(str);
ss<<n;
string temp = ss.str();
or we can get the concatenated string by following logic
string temp = str + to_string(n);
Now we can get the QString from string:
QString str = QString::fromUtf8(temp.c_str());

Related

Regex - Private subtags RFC5646

Can someone please help me with a regex to pull out subtags from a RFC5646?
Example strings
en-us-x-test-test1 = test,test1
en-gb-x-test-test2 = test,test2
fr-x-test-test3 = test,test3
I'm using a QRegExp
Thanks for any assistance
You don't need a regex here. Split your input by - then take the last two string and add a coma in between:
QString str = "en-us-x-test-test1";
QStringList list = str.split('-');
QString output = list.at(list.count()-2) + "," + list.at(list.count()-1);
Of course, you have to check for list length to avoid index error.

C++ Strings and delimiters [duplicate]

This question already has answers here:
How do I iterate over the words of a string?
(84 answers)
Closed 7 years ago.
So i'm doing a program where i input some data into a file and read from it. The problem is i dont know how to handle this. I read from the file and recieve a string containing alot of different data that is seperated by a delimiter "|".
string data ="FirstName|LastName|Signature|Height";
So my question is, is there a way to seperate this string nicely and store each of the values in a seperate variable?
p.s I have tried this so far. I did find this function subrt() and find() which i could use in order to find the delimiter and take out a value but it doesnt give the correct value for me so i think i'm doing something wrong with it. Only the fname value is correct.
const string DELIM = "|";
string fname = data.substr(0, data.find(DELIM));
string lname = data.substr(1, data.find(DELIM));
string signature = data.substr(2, data.find(DELIM));
string height = data.substr(3, data.find(DELIM));
You did not understand how substr() works. The first parameter is not the number of character found, it is the index at which to start. See the doc. You should do the same for the find function. Something like that:
string const DELIM = "|";
auto const firstDelim = data.find(DELIM);
auto const secondDelim = data.find(DELIM, firstDelim + 1); // begin after the first delim
// and so on
auto fname = data.substr(0, firstDelim);
auto lname = data.substr(firstDelim + 1, secondDelim);
// and so on

Verify and cut a string using regexp in matlab

I have the following string:
{'output',{'variable','VGRG_Pos_Var1/Parameters/D_foo'},'date',734704.60904050921}
I would like to verify the format of the string that the word 'variable' is the second word and i would like to retrive the string after the last '/' in the 3rd string (In this example 'D_foo').
how could i verify this and retrive the sting i search?
I tried the following:
regexp(str,'{''\w+'',{''variable'',''([(a-z)|(A-Z)|/|_])+')
without success
REMARK
The string to analysis is not splited after the komma, it is only due to length of the string.
EDIT
my string is:
'{''output'',{''variable'',''VGRG_Pos_Var1/Parameters/D_foo''},''date'',734704.60904050921}';
and not a cell, which could be understood. I added the sybol ' at the start and end of the string to symbolizied that it is a string.
I realise that you mention using regexp in the question, but I'm not sure if this is a requirement? If other solutions are acceptable you could try this:
str='{''output'',{''variable'',''VGRG_Pos_Var1/Parameters/D_foo''},''date'',734704.60904050921}';
parts1=textscan( str, '%s','delimiter',{',','{','}'},'MultipleDelimsAsOne',1);
parts2=textscan( parts1{1}{3}, '%s','delimiter',{'/',''''},'MultipleDelimsAsOne',1);
string=parts2{1}{end}
match=strcmp(parts1{1}{2},'variable')
To answer the first part of your question, you can write this:
str = {'output',{'variable','VGRG_Pos_Var1/Parameters/D_foo'},'date',734704.60904050921};
temp = str(2); %this holds the cell containing the two strings
if cmpstr(temp{1}(1), 'variable')
%do stuff
end
For the second part you can do this:
str = {'output',{'variable','VGRG_Pos_Var1/Parameters/D_foo'},'date',734704.60904050921};
temp = str(2); %like before, this contains the cell
temp = temp{1}(2); %this picks out the second string in the cell
temp = char(temp); %turns the item from a cell to a string
res = strsplit(temp, '/'); %splits the string where '/' are found, res is an array of strings
string = res(3); %assuming there will always be just 2 '/'s.

Print a Hyphen/Dash?

How to print hyphens to an output like this for ex, 344-34-4333. If this ID is read from a file that has the number without hyphens, how can I get it to print xxx-xx-xxxx 3 to 2 to 4 ?
The std::string class has a lot of constructors to help you with these problems. The substr() member function is also useful.
A quick and dirty example:
std::string x("344344333");
std::string res = x.substr(0, 3) + '-' + s.substr(3, 2) + '-' + x.substr(5,4);
For more complex strings, you'll probably prefer to use the std::ostringstream class.

VB.Net help selecting first index of string with regex

I was wondering if there was a way I could start a selection from the Regex string i have in the below example
The below example works exactly how I want it too however if there is text that matches before it on another line it is choosing the wrong text and highlighting it.
What im wondering is if there is a way to get the start index of the regex string?
If Regex.IsMatch(Me.TextBox1.Text, "\b" + Regex.Escape("is") + "\b") Then
Me.TextBox1.SelectionStart = Me.TextBox1.Text.IndexOf("is")
Dim linenumber As Integer = Me.TextBox1.GetLineFromCharIndex(Me.TextBox1.Text.IndexOf("is"))
Me.TextBox1.SelectionLength = Me.TextBox1.Lines(linenumber).Length
Me.TextBox1.Focus()
Me.TextBox1.SelectedText = "is " & Me.TextBox2.Text
The System.Text.RegularExpression.Match object has a property which should help you here: Match.Index. Match.Index will tell you where the capture starts, and Match.Length tells you how long it is. Using those you could change your code to look like this:
If Regex.IsMatch(Me.TextBox1.Text, "\b" + Regex.Escape("is") + "\b") Then
Dim m as Match
m = Regex.Match(Me.TextBox1.Text, "\b" + Regex.Escape("is") + "\b")
Me.TextBox1.SelectionStart = m.Index
Dim linenumber As Integer = Me.TextBox1.GetLineFromCharIndex(m.Index)
Me.TextBox1.SelectionLength = Me.TextBox1.Lines(linenumber).Length
Me.TextBox1.Focus()
Me.TextBox1.SelectedText = "is " & Me.TextBox2.Text