How do I remove a comma off the end of a string?
$string = rtrim($string, ',');
Docs for rtrim
here
Remove last comma (and possible whitespaces after the last comma) from the end of a string
This will remove the last comma and any whitespace after it:
str = str.replace(/,\s*$/, "");
It uses a regular expression:
The
/
mark the beginning and end of the regular expressionThe
,
matches the commaThe
\s
means whitespace characters (space, tab, etc) and the*
means 0 or moreThe
$
at the end signifies the end of the string
How can I remove the last comma from a string in Perl
Try this
$value =~ s/,\s*$//;
The pattern ,\s*$
matches a comma (,
) followed by zero or more space-chars (\s*
), followed by the end of the line/input ($
).
How to remove the last comma from each line in a string?
This is not too good in terms of performance if your string is very long, but it should do
"\n".join(x[:-1] for x in output.splitlines())
Remove multiple commas from start and end of string
You could match the part between the commas:
const re = /^,*(.*?),*$/;const data = ',,,^^ranbir$$this is first comment,,';const result = data.match(re);
console.log(result[1]);
Remove the last comma in a string
Use a regular expression? Assuming your data frame is DF
:
DF$state <- gsub(",$", "", DF$state)
The regular expression ,$
means every comma that occurs at the end of a string. The command gsub
replaces every instance of the first argument with the second argument (in this case, nothing) that occurs in the third argument (DF$state
).
how to remove comma if string having comma at end
You could try a regular expression:
names = names.replaceAll(",$", "");
Or a simple substring:
if (names.endsWith(",")) {
names = names.substring(0, names.length() - 1);
}
How can I remove last comma and all spaces from string, then sanitize it using PHP?
Replace spaces and trim commas and spaces at beginning or end:
$result = str_replace(' ', '', trim($string, ', '));
Or:
$result = trim(str_replace(' ', '', $string), ',');
Then if you only want numbers and commas (no letters etc.) maybe:
if(!preg_match('/^[\d,]+$/', $string)) {
//error
}
However this will not error on a single number with no commas.
How to remove last comma from print(string, end=“, ”)
You could build a list of strings in your for loop and print afterword using join
:
strings = []
for ...:
# some work to generate string
strings.append(sting)
print(', '.join(strings))
alternatively, if your something
has a well-defined length (i.e you can len(something)
), you can select the string terminator differently in the end case:
for i, x in enumerate(something):
#some operation to generate string
if i < len(something) - 1:
print(string, end=', ')
else:
print(string)
UPDATE based on real example code:
Taking this piece of your code:
value = input("")
string = ""
for unit_value in value.split(", "):
if unit_value.split(' ', 1)[0] == "negative":
neg_value = unit_value.split(' ', 1)[1]
string = "-" + str(challenge1(neg_value.lower()))
else:
string = str(challenge1(unit_value.lower()))
print(string, end=", ")
and following the first suggestion above, I get:
value = input("")
string = ""
strings = []
for unit_value in value.split(", "):
if unit_value.split(' ', 1)[0] == "negative":
neg_value = unit_value.split(' ', 1)[1]
string = "-" + str(challenge1(neg_value.lower()))
else:
string = str(challenge1(unit_value.lower()))
strings.append(string)
print(', '.join(strings))
Related Topics
Get PHP Class Property by String
Fastest Way to Serve a File Using PHP
Right Way to Escape Backslash [ \ ] in PHP Regex
PHP: Merge Two Arrays While Keeping Keys Instead of Reindexing
Why Is Json_Encode Adding Backslashes
Is There a Built-In Way to Get All of the Changed/Updated Fields in a Doctrine 2 Entity
What Exactly Are Late Static Bindings in PHP
Manipulate a String That Is 30 Million Characters Long
JavaScript Equivalent of PHP'S In_Array()
How to Avoid Echoing Character 65279 in PHP
Get Data from Json File With PHP