trim

How to remove trailing and leading whitespace for user-provided input in a batch file?

江枫思渺然 提交于 2019-11-26 20:30:20
I know how to do this when the variable is pre-defined. However, when asking for the user to enter in some kind of input, how do I trim leading and trailing whitespace? This is what I have so far: @echo off set /p input=: echo. The input is %input% before ::trim left whitespace for /f "tokens=* delims= " %%a in ("%input%") do set input=%%a ::trim right whitespace (up to 100 spaces at the end) for /l %%a in (1,1,100) do if "!input:~-1!"==" " set input=!input:~0,-1! echo. The input is %input% after pause You need to enable delayed expansion. Try this: @echo off setlocal enabledelayedexpansion

Trim characters in Java

荒凉一梦 提交于 2019-11-26 20:27:33
How can I trim characters in Java? e.g. String j = “\joe\jill\”.Trim(new char[] {“\”}); j should be "joe\jill" String j = “jack\joe\jill\”.Trim("jack"); j should be "\joe\jill\" etc Colin Gislason Apache Commons has a great StringUtils class (org.apache.commons.lang.StringUtils). In StringUtils there is a strip(String, String) method that will do what you want. I highly recommend using Apache Commons anyway, especially the Collections and Lang libraries. This does what you want: public static void main (String[] args) { String a = "\\joe\\jill\\"; String b = a.replaceAll("\\\\$", "")

Trim spaces from end of a NSString

会有一股神秘感。 提交于 2019-11-26 19:16:08
I need to remove spaces from the end of a string. How can I do that? Example: if string is "Hello " it must become "Hello" Dan Use -stringByTrimmingCharactersInSet: NSString *string = @" this text has spaces before and after "; NSString *trimmedString = [string stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; (This will trim whitespace characters on both ends). Swift 3 let string = " this text has spaces before and after " let trimmedString = string.trimmingCharacters(in: .whitespacesAndNewlines) Matt H. Another solution involves creating mutable string: /

Replace all whitespace characters

回眸只為那壹抹淺笑 提交于 2019-11-26 18:54:43
问题 I want to replace all occurrences of white space characters (space, tab, newline) in JavaScript. How to do so? I tried: str.replace(/ /gi, "X") 回答1: You want \s Matches a single white space character, including space, tab, form feed, line feed. Equivalent to [ \f\n\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff] in Firefox and [ \f\n\r\t\v] in IE. str = str.replace(/\s/g, "X"); 回答2: \s is a meta character that covers all white space. You don't need to make it case

String strip() for JavaScript? [duplicate]

久未见 提交于 2019-11-26 18:54:17
问题 This question already has an answer here: Trim string in JavaScript? 26 answers What's a clean and efficient JavaScript implementation to strip leading and trailing spaces from a string? For example: " dog" "dog " " dog " " dog " all get turned into "dog" 回答1: Use this: if(typeof(String.prototype.trim) === "undefined") { String.prototype.trim = function() { return String(this).replace(/^\s+|\s+$/g, ''); }; } The trim function will now be available as a first-class function on your strings.

How to remove all white space from the beginning or end of a string?

寵の児 提交于 2019-11-26 18:39:41
How can I remove all white space from the beginning and end of a string? Like so: "hello" returns "hello" "hello " returns "hello" " hello " returns "hello" " hello world " returns "hello world" Mau String.Trim() returns a string which equals the input string with all white-spaces trimmed from start and end: " A String ".Trim() -> "A String" String.TrimStart() returns a string with white-spaces trimmed from the start: " A String ".TrimStart() -> "A String " String.TrimEnd() returns a string with white-spaces trimmed from the end: " A String ".TrimEnd() -> " A String" None of the methods modify

JavaScript chop/slice/trim off last character in string

我是研究僧i 提交于 2019-11-26 18:31:13
问题 I have a string, 12345.00 , and I would like it to return 12345.0 . I have looked at trim , but it looks like it is only trimming whitespace and slice which I don't see how this would work. Any suggestions? 回答1: You can use the substring function: let str = "12345.00"; str = str.substring(0, str.length - 1); console.log(str); This is the accepted answer, but as per the conversations below, the slice syntax is much clearer: let str = "12345.00"; str = str.slice(0, -1); console.log(str); 回答2:

Get URL from background-image Property

感情迁移 提交于 2019-11-26 18:24:23
问题 i am currently using this to get the url from the background-image property: var url = $(this).find('div').css('background-image'); url = url.substr(4, url.length - 5); This works fine however in some browser (IE6-9), instead of it being: url(http://.com/) its url("http://.com/) Is there a failsafe way that will just get the url from this property? without having to do browser detection or some other stuff? 回答1: You could do: url = url.replace(/^url\(["']?/, '').replace(/["']?\)$/, ''); This

Find svg viewbox that trim whitespace around

前提是你 提交于 2019-11-26 17:47:44
问题 Assuming I have an svg that draws some paths, what tools should I use to find a viewbox that perfectly fits those paths so that all redundant space around is trimmed? 回答1: You can simply set your svg's viewBox to its Bounding Box. function setViewbox(svg) { var bB = svg.getBBox(); svg.setAttribute('viewBox', bB.x + ',' + bB.y + ',' + bB.width + ',' + bB.height); } document.querySelector('button').addEventListener('click', function() { setViewbox(document.querySelector('svg')); }); svg {

Does swift have a trim method on String?

血红的双手。 提交于 2019-11-26 16:56:32
Does swift have a trim method on String? For example: let result = " abc ".trim() // result == "abc" Sivanraj M Here's how you remove all the whitespace from the beginning and end of a String . (Example tested with Swift 2.0 .) let myString = " \t\t Let's trim all the whitespace \n \t \n " let trimmedString = myString.stringByTrimmingCharactersInSet( NSCharacterSet.whitespaceAndNewlineCharacterSet() ) // Returns "Let's trim all the whitespace" (Example tested with Swift 3+ .) let myString = " \t\t Let's trim all the whitespace \n \t \n " let trimmedString = myString.trimmingCharacters(in: