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.
How to remove portion of string after certain character for each element of a Pandas series (or list)?
Using str.split
Ex:
s = pd.Series( ['AAA.B', 'BBB.C', 'CCC.D'])
print(s.str.split(".").str[0])
Output:
0 AAA
1 BBB
2 CCC
dtype: object
Remove portion of a string after a particular character from the end of the string
Use strrpos
to get position of last matching element
//$variable = "8233 Station #2212"; //8233 Station
$variable = "8233 Station #2211 #2212";
echo trim(substr($variable, 0, strrpos($variable, "#"))); //8233 Station #2211
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';
}
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.
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 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.
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 remove all characters before a specific character in Java?
You can use .substring()
:
String s = "the text=text";
String s1 = s.substring(s.indexOf("=") + 1);
s1.trim();
then s1
contains everything after =
in the original string.
s1.trim()
.trim()
removes spaces before the first character (which isn't a whitespace, such as letters, numbers etc.) of a string (leading spaces) and also removes spaces after the last character (trailing spaces).
Delete all characters after a certain character from a string in Swift
You can use StringProtocol
method range(of string:)
, get the resulting range lowerBound, create a PartialRangeUpTo
with it and subscript the original string:
Swift 4 or later
let word = "orange"
if let index = word.range(of: "n")?.lowerBound {
let substring = word[..<index] // "ora"
// or let substring = word.prefix(upTo: index) // "ora"
// (see picture below) Using the prefix(upTo:) method is equivalent to using a partial half-open range as the collection’s subscript.
// The subscript notation is preferred over prefix(upTo:).
let string = String(substring)
print(string) // "ora"
}
Related Topics
PHP: Storing 'Objects' Inside the $_Session
PHP Foreach Pass by Reference: Last Element Duplicating (Bug)
How to Show Ajax Requests in Url
How to Get Const'S Defined on a PHP Class
PHP Error: Cannot Modify Header Information - Headers Already Sent
How to Resolve Curl Error (7): Couldn't Connect to Host
Parsing Json Object in PHP Using Json_Decode
PHP Json_Decode() Returns Null With Valid Json
Create Programmatically a Woocommerce Product Variation With New Attribute Values
PHP Threading Call to a PHP Function Asynchronously
Get Everything Between ≪Tag≫ and ≪/Tag≫ With PHP
How to Properly Use a Pdo Object For a Parameterized Select Query
The Holy Grail of Cleaning Input and Output in PHP