Remove a random expression from column [duplicate] - sql

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Remove a random expression from string
I have a column which contains values like this
"000003023_AggregateStopLossLimit_W x3A 973911_2012-12-22.PDF";
I want to create a substring which doesn't have the part x3A 973911 in it.
Whic means I want something like this,
000003023_AggregateStopLossLimit_W_2012-12-22.PDF
The value x3A 973911 is not constant, so basically, in words, I want the part of string to be removed which comes after the first space and ends at the next '_'.
Any ideas ?

String phrase="000003023_AggregateStopLossLimit_W x3A 973911_2012-12-22.PDF";
phrase.replace("x3A 973911","");
//am not sure if you have to trim() but i guess this will answer your question.

Related

What is the right method for a Random List of Sub Values From a List? Kotlin [duplicate]

This question already has answers here:
Take n random elements from a List<E>?
(12 answers)
Closed 1 year ago.
What is the correct code for getting a realy random sublist (with size X) of a List with Integers?
{1,2,5,7,12,18,71,72,73} -> get a sublist e.g with 4 items
->Result: {1,5,71,73}
I was trying to solve it with Random.nextInt, but as my first list is not in a row, it's not possible.
What would be the correct solution?
If we can take the same element repeatedly:
List(x) { list.random() }
If we cannot:
list.asSequence()
.shuffled()
.take(x)
.toList()

Find and replace numeric values preceding a substring [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a dataframe which looks as following:
df['col1'].values
array(['cat 113kd29', 'do56goat24kdasd', 'pig145kd'])
I need to create a new column df['vals'] with following values:
cat 29
do56goatasd
pig
i.e. first I need to look for substring kd and then find the numeric value preceding it. I am not sure how to go about this.
There can be multiple numeric values in each string so I need to find only ones before kd. Please note the string 'cat 113kd29'. Also look at 'do56goat24kdasd'
I tried the following but it didn't work:
df['col1'].str.replace(r'(\d+)kd', '')
Your call to str.replace is correct, but you need to assign it to the original Pandas column on the left hand side of an assignment:
df["col1"] = df["col1"].str.replace(r'\d+kd', '')
Note that str.replace does a global replacement by default, so there is no need to use any sort of flag.
Another way is to match digits precedingkd and kd and replace it with nothing
df["col1"]=df.col1.str.replace('\d+kd\Z','', regex=True)

VB.NET Substring function broken [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a problem with Substring function. As you can see on Watch window shcreenshot I have the variable called val equal to ‎03.‎09.‎2015 ‏‎17:30
I do not understand why but
val.Substring(0,2) returns 0 instead of 03
val.Substring(0,3) returns 03 (string of two symbols)
What am I doing wrong?
Your string contains non-printable characters. Note the following from your screenshot:
val = "03.09.2015 17:30"
val.Length = 21
However, 03.09.2015 17:30 only has 16 characters. Thus, the string contains other, zero-width characters.
To find the culprit, output a hex dump of your problematic string and compare it with the hex dump of the literal string 03.09.2015 17:30.
From OP comment:
The original problem was with Parse (and Parse-like) functions. It can not parse this string as Date or DateTime
Once the invisible characters are removed from the string, it certainly can be parsed:
Dim d As Date = DateTime.Parse("03.09.2015 17:30", Globalization.CultureInfo.InvariantCulture)
Dim m As Integer = d.Month ' m = 3
Of course, choose the particular Parse method that best fits your needs.

SQL remove spaces between specific character in a string?

I want to update a database table field using another field in the same table. Currently I have this table called sources.
Name Code
In the name column I have values like this example :
' Deals On Wheels '
'Homesru - Abu Dhabi - Madinat Zayed Gold Centre'
And I am having this update statement :
UPDATE Sources
SET Code = REPLACE((LTRIM(RTRIM(Name))),' ','-')
the result is :
Deals-On-Wheels-Al-Aweer
which is fine.
but for second one I have this :
Homesru---Abu-Dhabi---Madinat-Zayed-Gold-Centre
I want it to be like this :
Homesru-Abu-Dhabi-Madinat-Zayed-Gold-Centre
How can I Achieve this ? Any Help is appreciated.
As suggested by #DanielE. my answer will point to a more global solution, in case you ever need to replace duplicated/triplicated/quadriplicated/... occurrences of a character on a string.
I'll not create a full solution for this issue, is a recurring question and there are really good solutions around already. Check these links:
SQL Server Central: remove spaces between specific character in a string?. This forum post will point to the next link I'm posting here. But is good to know what they are asking and answering.
Replace multiple spaces with new one but you can slightly modify it to replace any character you want.
You can also rely on this answer Find and remove repeated strings from Aaron Bertrand.
try
REPLACE((LTRIM(RTRIM(REPLACE((LTRIM(RTRIM(Name))),' - ','-')))),' ','-')
this will first replace ' - ' with just '-'
You might want to look into using a UDF to do a regular expression search and replace. See https://launchpad.net/mysql-udf-regexp

How can I compare a string variable to a string in xcode? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
String comparison in Objective-C
I realize that the question is not very specific. I am working on a simple trivia game to test a couple of things, right now it has 5 arrays, one for the questions, one for the first answer option, one for the second answer option, one for the third, and one that says which one is correct.
I have an if statement in that checks wether the button pressed matches the correct answer.
Answer2 is connected to the button that would select option b in my trivia app, strCorrect is the string array that holds the single character that says which option out of the three is right, intCurrentQuestion is just an integer I use to reference the index of the arrays.
-(IBAction)Answer2{
if ([strCorrect objectAtIndex:intCurrentQuestion] == [NSString stringWithFormat:#"B"]){
//do these things blah blah
}
}
The problem is that there are no errors when it is compiled but it doesn't work either. How do I go about making this work?
For testing purpose I am cheating and passing the strCorrect to a hidden label in the nib and then comparing the label text to #"B" and it works but its...well its just awful.
What you're doing in your code above is comparing the memory address of two strings, not their value. Do compare two NSStrings you have to do this:
[[strCorrect objectAtIndex:intCurrentQuestion] isEqualToString:#"B"];
[[strCorrect objectAtIndex:intCurrentQuestion] isEqualToString:#"B"]