Remove parentheses in QString - qt

I have a QString containing "(M001)" and I want to remove the parentheses in text. The result should be "M001". How should I use a QRegExp for this?

I see two possible way to do it:
1.Using QString::remove() like this:
str.remove("(");
str.remove(")");
2.Using QRegExp class like this:
str.remove(QRegExp("[()]"));
In both of variants I get "M001" string.
Of course, there are some restrictions: all parentheses will be removed.But seems like it's what you want, don't you?

If you know your string always has parenthesis, you could just do something like:
str = str.mid(1); // Remove first character
str.chop(1); // Remove last character
Otherwise you could also do this instead of using a regular expression:
if (str.startsWith('(') && str.endsWith(')')) {
str = str.mid(1); // Remove first character
str.chop(1); // Remove last character
}
But if you insist using a QRegExp, try this:
str.remove(QRegExp("^\\(|\\)$"));
or this:
str.replace(QRegExp("^\\((.*)\\)$"), "\\1");
EDIT: If you want to remove ALL parenthesis from the string you can try:
str.remove('(').remove(')');
or
str.remove(QRegExp("[()]"));

Related

Use Replace function to remove "{ characters from string

I would like to remove "{ and replace it with {. The following is the line of code that I'm currently using.
var MyString = DataString.Replace(#""{", "");
The following is the error message I'm getting
Please advise
Thanks
You need to escape the quote that you want to replace using two quotes, so for your example:
var MyString = DataString.Replace(#"""{", "{");
Also see How to include quotes in a string for alternatives to use quotes in strings.
If you are expecting JSON data then what you really need is a JSON Parser for that. And if you just want to replace "{ to { then you simply need to escape and replace the string like below:
// Suppose the variable named str has a value of Hello"{ wrapped in double quotes
var strReplaced = str.Replace("\"{", "{");
Console.WriteLine($"strReplaced: {strReplaced}");
// This will result in strReplaced: Hello{

How to remove double qoutes in Objective-C

Let me introduce myself.
My name is Vladimir, C++ programmer, I am from Serbia. two weeks ago I have started to learn objective-C and it was fine until tonight.
Problem:
I cant remove double quotes from my NSLog output.
NSLog(#"The best singers:%#", list.best);
Strings are joined with componentsJoinedByString:#" and "
I would like to get something like this:
The best singers: Mickey and John.
But I get this:
The best singers: ("Mickey", and "John").
I cant remove comma (,) and parentheses either.
I have tried with "replaceOccurencesOfString" but with no success. It can remove any character except qoute and comma.
Also I have used -(NSString *)description method to return string.
You are getting the raw output from your list (which I assume is an array). You will have to do your own formatting to get this to display in the format that you want. You can achieve this by building your string by iterating through your array. Note that this probably isn't the most efficient nor the most robust way to achieve this.
NSMutableString *finalString = [NSMutableString string];
BOOL first = YES;
for (NSString *nameString in list) {
if (first) {
[finalString appendString:nameString];
first = NO;
} else {
[finalString appendString:[NSString stringWithFormat:#" and %#", nameString]];
}
}

asp.net querystring

I have the following page querystring:
register.aspx?id="jSmith"
I have the following code to retrieve the value of ID
string qString = string.IsNullOrEmpty(Request.QueryString["id"]) ? string.Empty : HttpUtility.UrlDecode(Request.QueryString["id"]);
When I view the value of qString I get something like
"\"jSmith\""
so when I do the following:
if (qString == "jSmith")
{
........
}
it does not not execute the if condition. What do I need to do s that it does not have the quotes.
The code is correct.
The problem is that you are passing to the page "jSmith" with the double quotes as part of the string.
Try invoke the page this way
register.aspx?id=jSmith
That is because the correct way to give the path in this case would be register.aspx?id=jSmith, without the quotes. If you need spaces, or other special characters, in your ID, these should be URL encoded (and will be decoded by your code), but not enclosed in quotes.
For example, if your id was the string john smith, the URL would become register.aspx?id=john+smith, since + is the URL encoding of a space.
You don't need to put quotes around values in querystring, by definition they're all strings...
Your querystring should look like :
register.aspx?id=jSmith
You do not need the quotation marks in your querystring.
It should read
register.aspx?id=jSmith
You should look for
if (qString == "\"jSmith\"")
the \ is escaping the extra "
or you could perform a replace to remove the extra "
use
Response.Redirect("Qstring.aspx?name= smith");
and on the page Qstring.aspx load event
string s=Request.QueryString["name"].ToString();
gives u "smith" in s variable

Replace string from character onwards

I've got a string like so
Jamie(123)
And I'm trying to just show Jamie without the brackets etc
All the names are different lengths so I was wondering if there was a simple way of replacing everything from the first bracket onwards?
Some others are displayed like this
Tom(Test(123))
Jack ((4u72))
I've got a simple replace of the bracket at the moment like this
mystring.Replace("(", "").Replace(")","")
Any help would be appreciated
Thanks
VB.NET
mystring.Substring(0, mystring.IndexOf("("C)).Trim()
C#
mystring.Substring(0, mystring.IndexOf('(')).Trim();
One logic; get the index of the ( and you can trim the later part from that position.
public static string Remove(string value)
{
int pos = value.IndexOf("(");
if (pos >= 0)
{
return value.Remove(pos, remove.Length);
}
return value;
}
aneal's will work. The alternative I generally use because it's a bit more flexible is .substring.
string newstring = oldstring.substring(0,oldstring.indexof("("));
If you aren't sure that oldstring will have a "(" you will have to do the test first just as aneal shows in their answer.
String.Remove(Int32) will do what you need:
Deletes all the characters from this string beginning at a
specified position and continuing through the last position.
You will also have to .Trim() as well given the data with padding:
mystring = mystring.Remove(mystring.IndexOf("("C))).Trim()

What is the regular expression for "No quotes in a string"?

I am trying to write a regular expression that doesn't allow single or double quotes in a string (could be single line or multiline string). Based on my last question, I wrote like this ^(?:(?!"|').)*$, but it is not working. Really appreciate if anybody could help me out here.
Just use a character class that excludes quotes:
^[^'"]*$
(Within the [] character class specifier, the ^ prefix inverts the specification, so [^'"] means any character that isn't a ' or ".)
Just use a regex that matches for quotes, and then negate the match result:
var regex = new Regex("\"|'");
bool noQuotes = !regex.IsMatch("My string without quotes");
Try this:
string myStr = "foo'baa";
bool HasQuotes = myStr.Contains("'") || myStr.Contains("\""); //faster solution , I think.
bool HasQuotes2 = Regex.IsMatch(myStr, "['\"]");
if (!HasQuotes)
{
//not has quotes..
}
This regular expression below, allows alphanumeric and all special characters except quotes(' and "")
#"^[a-zA-Z-0-9~+:;,/#&_#*%$!()\[\] ]*$"
You can use it like
[RegularExpression(#"^[a-zA-Z-0-9~+:;,/#&_#*%$!()**\[\]** ]*$", ErrorMessage = "Should not allow quotes")]
here use escape sequence() for []. Since its not showing in this post

Resources