string-formatting

Pad IP addresses with leading 0's - powershell

孤街醉人 提交于 2021-02-19 07:52:23
问题 I'm looking to pad IP addresses with 0's example 1.2.3.4 -> 001.002.003.004 50.51.52.53 -> 050.051.052.053 Tried this: [string]$paddedIP = $IPvariable [string]$paddedIP.PadLeft(3, '0') Also tried split as well, but I'm new to powershell... 回答1: You can use a combination of .Split() and -join . ('1.2.3.4'.Split('.') | ForEach-Object {$_.PadLeft(3,'0')}) -join '.' With this approach, you are working with strings the entire time. Split('.') creates an array element at every . character. .PadLeft

Pad IP addresses with leading 0's - powershell

最后都变了- 提交于 2021-02-19 07:52:07
问题 I'm looking to pad IP addresses with 0's example 1.2.3.4 -> 001.002.003.004 50.51.52.53 -> 050.051.052.053 Tried this: [string]$paddedIP = $IPvariable [string]$paddedIP.PadLeft(3, '0') Also tried split as well, but I'm new to powershell... 回答1: You can use a combination of .Split() and -join . ('1.2.3.4'.Split('.') | ForEach-Object {$_.PadLeft(3,'0')}) -join '.' With this approach, you are working with strings the entire time. Split('.') creates an array element at every . character. .PadLeft

center multi-line text in python output

大兔子大兔子 提交于 2021-02-18 18:06:02
问题 OK, so this seems like a really basic question, but I can't find a workable answer anywhere, so here goes. I have some text: text = ''' Come and see the violence inherent in the system. Help! Help! I'm being repressed! Listen, strange women lyin' in ponds distributin' swords is no basis for a system of government. Supreme executive power derives from a mandate from the masses, not from some farcical aquatic ceremony. The Lady of the Lake, her arm clad in the purest shimmering samite held

How to format message with argument names instead of numbers?

Deadly 提交于 2021-02-17 08:32:14
问题 I have something like: String text = "The user {0} has email address {1}." // params = { "Robert", "myemailaddr@gmail.com" } String msg = MessageFormat.format(text, params); This isn't great for me, because sometimes my translators are not sure what goes in the {0} and {1}, also it would be nice to be able to reword the messages without worrying about the order of the args. I'd like to replace the arguments with readable names instead of numbers. Something like this: String text = "The user

How to format message with argument names instead of numbers?

妖精的绣舞 提交于 2021-02-17 08:30:18
问题 I have something like: String text = "The user {0} has email address {1}." // params = { "Robert", "myemailaddr@gmail.com" } String msg = MessageFormat.format(text, params); This isn't great for me, because sometimes my translators are not sure what goes in the {0} and {1}, also it would be nice to be able to reword the messages without worrying about the order of the args. I'd like to replace the arguments with readable names instead of numbers. Something like this: String text = "The user

Fixing right alignment of string formatting

。_饼干妹妹 提交于 2021-02-17 06:42:14
问题 I'm trying to align the output of the list like shown: But it keeps coming out like this: My code for all of this is: subject_amount = int(input("\nHow many subject do you want to enrol? ")) class Subject: def __init__(self, subject_code, credit_point): self.subject_code = subject_code self.credit_point = credit_point subjects = [] for i in range(1, (subject_amount + 1)): subject_code = input("\nEnter subject " + str(i) + ": ") credit_point = int(input("Enter subject " + str(i) + " credit

Function to return a formatted string in python

◇◆丶佛笑我妖孽 提交于 2021-02-11 15:21:38
问题 A function which returns a formatted string by replacing all instances of %X with Xth argument in args (0...len(args)) Example: simple_format("%1 calls %0 and %2", "ashok", "hari")=="hari calls ashok and %2" Please help me out. 回答1: >>> "{1} calls {0} and {2}".format( "ashok", "hari", "tom") 'hari calls ashok and tom' If you really need the function simple_format , then: import re def simple_format(*args): s = re.sub(r'%(\d+)', r'{\1}', args[0]) return s.format(*args[1:]) Example: >>> simple

@Html.EditorFor DateTime not displaying when set a default value to it

自作多情 提交于 2021-02-07 11:42:17
问题 I'd like to set a default value to my model in Controller, But It cannot display in create page. TestModel code: public class TestModel { [DataType(DataType.DateTime), Required] [DisplayFormat(DataFormatString = "yyyy/MM/dd", ApplyFormatInEditMode = true)] public DateTime StartTime { get; set; } [DataType(DataType.DateTime), Required] [DisplayFormat(DataFormatString = "yyyy/MM/dd", ApplyFormatInEditMode = true)] public DateTime EndTime { get; set; } public string Description { get; set; } }

How to format a number with comma and specified precision digits in Python

回眸只為那壹抹淺笑 提交于 2021-02-07 11:32:37
问题 The question is for Python 2.6, that is what we have in production. I have this requirement for formatting a number (like 1234567.0987 or 1234567.0) with comma, and specified number of digits after decimal points. So, it if precision is three, 1234567.0987 may look like 1,234,567.099. I tried using Locale, as suggested by answers to many questions, the problem is that results in two digits after decimal, which is not acceptable for my requirement. I tried searching in other places, but did

Format a Number to a specific QString format

穿精又带淫゛_ 提交于 2021-02-07 11:25:20
问题 I have a question about formatting a decimal number to a certain QString format. Basically, I have an input box in my program that can take any values. I want it to translate the value in this box to the format "+05.30" (based on the value). The value will be limited to +/-99.99. Some examples include: .2 --> +00.02 -1.5 --> -01.50 9.9 --> +09.90 I'm thinking of using a converter like this, but it will have some obvious issues (no leading 0, no leading + sign). QString temp = QString::number