Remove Part of String After "."

Remove portion of a string after a certain character

$variable = substr($variable, 0, strpos($variable, "By"));

In plain english: Give me the part of the string starting at the beginning and ending at the position where you first encounter the deliminator.

Remove part of string after .

You just need to escape the period:

a <- c("NM_020506.1","NM_020519.1","NM_001030297.2","NM_010281.2","NM_011419.3", "NM_053155.2")

gsub("\\..*","",a)
[1] "NM_020506" "NM_020519" "NM_001030297" "NM_010281" "NM_011419" "NM_053155"

Remove part of string after character in an array and remove repeated elements

use Set & map:

var names =  ["ann_w", "james_q", "ann_q", "peter_p", "steve_q", "james_s"];
var unique_names = [ ...new Set(names.map(name => { return name.split('_')[0]}))]console.log(unique_names)

Remove characters after specific character in string, then remove substring?

For string manipulation, if you just want to kill everything after the ?, you can do this

string input = "http://www.somesite.com/somepage.aspx?whatever";
int index = input.IndexOf("?");
if (index >= 0)
input = input.Substring(0, index);

Edit: If everything after the last slash, do something like

string input = "http://www.somesite.com/somepage.aspx?whatever";
int index = input.LastIndexOf("/");
if (index >= 0)
input = input.Substring(0, index); // or index + 1 to keep slash

Alternately, since you're working with a URL, you can do something with it like this code

System.Uri uri = new Uri("http://www.somesite.com/what/test.aspx?hello=1");
string fixedUri = uri.AbsoluteUri.Replace(uri.Query, string.Empty);

Remove part of string after a specific character in Python

You are close, need str[0] for select lists and then add d:

df['time'] = df['time'].str.split('d').str[0].add('d')

Or:

df['time'] = df['time'].str.split('(d)').str[:2].str.join('')

print (df)
time
0 2019Y8m16d
1 2019Y9m3d
2 2019Y9m3d
3 2019Y9m3d

Or use Series.str.extract:

df['time'] = df['time'].str.extract('(.+d)')
print (df)
time
0 2019Y8m16d
1 2019Y9m3d
2 2019Y9m3d
3 2019Y9m3d

Flutter - Remove String after certain character?

You can use the subString method from the String class

String s = "one.two";

//Removes everything after first '.'
String result = s.substring(0, s.indexOf('.'));
print(result);

In case there are more than one '.' in the String it will use the first occurrance. If you need to use the last one (to get rid of a file extension, for example) change indexOf to lastIndexOf. If you are unsure there is at least one occurrance, you should also add some validation to avoid triggering an exception.

String s = "one.two.three";

//Remove everything after last '.'
var pos = s.lastIndexOf('.');
String result = (pos != -1)? s.substring(0, pos): s;
print(result);

How to remove anything in a string after -?

No need for regex. You can use explode:

$str = array_shift(explode('-', $str));

or substr and strpos:

$str = substr($str, 0, strpos($str, '-'));

Maybe in combination with trim to remove leading and trailing whitespaces.

Update: As @Mark points out this will fail if the part you want to get contains a -. It all depends on your possible input.

So assuming you want to remove everything after the last dash, you can use strrpos, which finds the last occurrence of a substring:

$str = substr($str, 0, strrpos($str, '-'));

So you see, there is no regular expression needed ;)

Remove part of a string after a character until a charcter and repeat until the string end

Removing everything after and including a character in a value is quite simple, just use LEFT and CHARINDEX:

LEFT(DS.Item,CHARINDEX('+',DS.Item + '+')-1)

The real problem you have is a little more complex:

  1. You have a denormalised design, making this far more difficult.
  2. You want to retain the denormalised design in the results, making this difficult again
  3. You are using a version of SQL Server that is very close to end of support that has no in built support for string splitting and aggregation.

Saying that, unless you were using Azure SQL Database (or somehow had a copy of SQL Server 2022) I wouldn't suggest STRING_SPLIT, as it doesn't provide an ordinal position parameter. Though STRING_AGG would make things far easier; and you could use a JSON splitter too.

Instead I use DelimitedSplit8K_LEAD here, and then "Ye Olde FOR XML PATHe (and STUFF)" for the string aggregation. This gives this clunky solution:

SELECT *
INTO dbo.YourTable
FROM (VALUES('095930'),
('CF0010+EN'),
('060983+PS'),
('086588+GG;086326+GG'),
('900010;'),
('CF0002;;CF0018;'))V(YourString);

GO

SELECT YT.YourString,
STUFF((SELECT ';' + LEFT(DS.Item,CHARINDEX('+',DS.Item + '+')-1)
FROM dbo.DelimitedSplit8K_LEAD(YT.YourString,';') DS
ORDER BY DS.ItemNumber
FOR XML PATH(''),TYPE).value('(./text())[1]','varchar(8000)'),1,1,'') AS NewString
FROM dbo.YourTable YT;


GO
DROP TABLE dbo.YourTable;

If you were using Azure SQL Database (or SQL Server 2022) then the answer would be much simpler:

SELECT YT.YourString,
STRING_AGG(LEFT(SS.Value,CHARINDEX('+',SS.Value + '+')-1),';') WITHIN GROUP (ORDER BY SS.Ordinal) AS NewString
FROM dbo.YourTable YT
CROSS APPLY STRING_SPLIT(YT.YourString,';',1) SS
GROUP BY YT.YourString; --Assuming YourString has a unique value

conditionally remove part of a string after a character in R

gsub("\n.*","",names)
gsub(" .*","",names)

You want the asterisk quantifier to apply to the dot (which is a wildcard matching all characters). Your version applied the quantifer to the newline or space character, so you were removing only strings of consecutive newlines or spaces.

Teradata: how to remove part of a string after special character

One method is to use regexp_substr():

select regexp_substr(col, '^[^/]*')


Related Topics



Leave a reply



Submit