How to Delete Everything in a String After a Specific Character

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);

How to delete everything after a certain character in a string?

Just take the first portion of the split, and add '.zip' back:

s = 'test.zip.zyz'
s = s.split('.zip', 1)[0] + '.zip'

Alternatively you could use slicing, here is a solution where you don't need to add '.zip' back to the result (the 4 comes from len('.zip')):

s = s[:s.index('.zip')+4]

Or another alternative with regular expressions:

import re
s = re.match(r'^.*?\.zip', s).group(0)

How to remove all characters after a specific character in python?

Split on your separator at most once, and take the first piece:

sep = '...'
stripped = text.split(sep, 1)[0]

You didn't say what should happen if the separator isn't present. Both this and Alex's solution will return the entire string in that case.

delete all characters after specific character, not character itself, in whole column, python

this solution convert int to string then search for first number different number from zero then take the partition you need (from begining to the position of the number different from zero) and convert it back to float. happy coding

df1['index']=[float(x[:x.find(x.split(".")[-1].replace("0","")[0])+1]) for x in list(map(str,df1['index']))]

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.

Removing the end of a string after a certain character in C

In C, When you write literal strings like this:
char* foo= "/one/two/three/two";

Those are immutable, which means they are embedded in the executable and are read only.

You get an access violation (crash) when trying to modify read only data.

Instead you can declare your string as an array of chars instead of a literal string.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void deleteEnd (char* myStr){

printf ("%s\n", myStr);
char *del = &myStr[strlen(myStr)];

while (del > myStr && *del != '/')
del--;

if (*del== '/')
*del= '\0';

return;
}

int main ( void )
{

char foo[] = "/one/two/three/two";
deleteEnd(foo);
printf ("%s\n", foo);

return 0;
}

How to delete everything in a string after a specific character?

There's no reason to use an external tool such as sed for this; bash can do it internally, using parameter expansion:

If the character you want to trim after is :, for instance:

$ str=foo_bar:baz
$ echo "${str%%:*}"
foo_bar

You can do this in both greedy and non-greedy ways:

$ str=foo_bar:baz:qux
$ echo "${str%:*}"
foo_bar:baz
$ echo "${str%%:*}"
foo_bar

Especially if you're calling this inside a tight loop, starting a new sed process, writing into the process, reading its output, and waiting for it to exit (to reap its PID) can be substantial overhead that doing all your processing internal to bash won't have.


Now -- often, when wanting to do this, what you might really want is to split a variable into fields, which is better done with read.

For instance, let's say that you're reading a line from /etc/passwd:

line=root:x:0:0:root:/root:/bin/bash
IFS=: read -r name password_hashed uid gid fullname homedir shell _ <<<"$line"
echo "$name" # will emit "root"
echo "$shell" # will emit "/bin/bash"

Even if you want to process multiple lines from a file, this too can be done with bash alone and no external processes:

while read -r; do
echo "${REPLY%%:*}"
done <file

...will emit everything up to the first : from each line of file, without requiring any external tools to be launched.

Delete everything after part of a string

For example, you could do:

String result = input.split("-")[0];

or

String result = input.substring(0, input.indexOf("-"));

(and add relevant error handling)

Remove everything after a certain character

You can also use the split() function. This seems to be the easiest one that comes to my mind :).

url.split('?')[0]

jsFiddle Demo

One advantage is this method will work even if there is no ? in the string - it will return the whole string.

Remove text from string after a certain character in C

You can use strchr:

char str[] = "89f81a03eb30a03c8708dde38cf:000391716";
char *ptr;

ptr = strchr(str, ':');
if (ptr != NULL) {
*ptr = '\0';
}


Related Topics



Leave a reply



Submit