string-formatting

Add separator to string at every N characters?

徘徊边缘 提交于 2019-11-27 00:08:33
I have a string which contains binary digits. How to separate string after each 8 digit? Suppose the string is: string x = "111111110000000011111111000000001111111100000000"; I want to add a separator like ,(comma) after each 8 character. output should be : "11111111,00000000,11111111,00000000,11111111,00000000," Then I want to send it to a list<> last 8 char 1st then the previous 8 chars(excepting ,) and so on. How can I do this? Regex.Replace(myString, ".{8}", "$0,"); If you want an array of eight-character strings, then the following is probably easier: Regex.Split(myString, "(?<=^(.{8})+)"

How do I format a number with a variable number of digits in Python? [duplicate]

a 夏天 提交于 2019-11-27 00:02:58
问题 This question already has answers here : Format string dynamically (5 answers) Closed 10 months ago . Say I wanted to display the number 123 with a variable number of padded zeroes on the front. For example, if I wanted to display it in 5 digits I would have digits = 5 giving me: 00123 If I wanted to display it in 6 digits I would have digits = 6 giving: 000123 How would I do this in Python? 回答1: There is a string method called zfill: >>> '12344'.zfill(10) 0000012344 It will pad the left side

Format numbers to strings in Python

孤街浪徒 提交于 2019-11-27 00:01:09
I need to find out how to format numbers as strings. My code is here: return str(hours)+":"+str(minutes)+":"+str(seconds)+" "+ampm Hours and minutes are integers, and seconds is a float. the str() function will convert all of these numbers to the tenths (0.1) place. So instead of my string outputting "5:30:59.07 pm", it would display something like "5.0:30.0:59.1 pm". Bottom line, what library / function do I need to do this for me? Starting with Python 3.6, formatting in Python can be done using formatted string literals or f-strings : hours, minutes, seconds = 6, 56, 33 f'{hours:02}:{minutes

Format a JavaScript string using placeholders and an object of substitutions?

我是研究僧i 提交于 2019-11-26 23:59:52
问题 I have a string with say: My Name is %NAME% and my age is %AGE%. %XXX% are placeholders. We need to substitute values there from an object. Object looks like: {"%NAME%":"Mike","%AGE%":"26","%EVENT%":"20"} I need to parse the object and replace the string with corresponding values. So that final output will be: My Name is Mike and my age is 26. The whole thing has to be done either using pure javascript or jquery. 回答1: The requirements of the original question clearly couldn't benefit from

Output single character in C

孤街醉人 提交于 2019-11-26 23:00:01
问题 When printing a single character in a C program, must I use "%1s" in the format string? Can I use something like "%c"? 回答1: yes, %c will print a single char: printf("%c", 'h'); also, putchar / putc will work too. From "man putchar": #include <stdio.h> int fputc(int c, FILE *stream); int putc(int c, FILE *stream); int putchar(int c); * fputc() writes the character c, cast to an unsigned char, to stream. * putc() is equivalent to fputc() except that it may be implemented as a macro which

String.Format alternative in C++ [duplicate]

丶灬走出姿态 提交于 2019-11-26 22:50:56
问题 This question already has an answer here: std::string formatting like sprintf 39 answers I don't have much experience working with C++. Rather I have worked more in C# and so, I wanted to ask my question by relating to what I would have done in there. I have to generate a specific format of the string, which I have to pass to another function. In C#, I would have easily generated the string through the below simple code. string a = "test"; string b = "text.txt"; string c = "text1.txt"; String

How can I extract keywords from a Python format string?

末鹿安然 提交于 2019-11-26 22:45:42
I want to provide automatic string formatting in an API such that: my_api("path/to/{self.category}/{self.name}", ...) can be replaced with the values of attributes called out in the formatting string. How do I extract the keyword arguments from a Python format string: "non-keyword {keyword1} {{escaped brackets}} {} {keyword2}" => 'keyword1', 'keyword2' You can use the string.Formatter() class to parse out the fields in a string, with the Formatter.parse() method : from string import Formatter fieldnames = [fname for _, fname, _, _ in Formatter().parse(yourstring) if fname] Demo: >>> from

Html.DisplayFor decimal format?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 22:39:26
I have a decimal value for example: 59625879,00 I want to show this value like this: 59.625,879 or 59625,879 How can I do this with @Html.DisplayFor(x => x.TAll, String.Format()) ? Thanks. Decorate your view model property with the [DisplayFormat] attribute and specify the desired format: [DisplayFormat(DataFormatString = "{0:N}", ApplyFormatInEditMode = true)] public decimal TAll { get; set; } and then in your view: @Html.DisplayFor(x => x.TAll) Another possibility if you don't want to do this on the view model you could also do it inside the view: @Model.tAll.ToString("N") but I would

Implementing String.Format() in VB6

血红的双手。 提交于 2019-11-26 22:10:20
Can String.Format() be implemented in VB6, at least a close-enough version of it that could be useful when programming in good ol' VB6? Good resource on the matter of VB6 string manipulation performance: http://www.aivosto.com/vbtips/stringopt2.html On a related not, I also came up with a couple string comparison functions, find them here on CodeReview.SE These functions are tremendously useful for improving VB6 readability, especially if you've been spoiled with .net code lately and suddenly are required to dive into a VB6 code base... Enjoy! I couldn't find one anywhere, so I made my own:

Converting a date string into UTC+0530 format using javascript

别来无恙 提交于 2019-11-26 22:08:40
问题 I have a date in the format 14-Feb-2011 , but I want to convert it into the format Mon Feb 14 10:13:50 UTC+0530 2011 . How Can I achieve this? 回答1: Using new Date(Date.UTC(year, month, day, hour, minute, second)) you can create a Date-object from a specific UTC time. I tried this code and it returned proper date (In Indian Locale) var d=Date.parse("14,Feb,2011"); document.write(new Date(d)); Output: Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time) . Here's an example of converting