高级字符串用法

这是strings.xml里面的代码,如下

<string name=”personal_welcome_message”>Welcome %s!</string>
<plurals name=”inbox_message_count”>
	<item quantity=”zero”>Your inbox is completely empty!</item>
	<item quantity=”one”>You one message in your inbox!</item>
	<item quantity=”two”>You two messages waiting to be read!</item>
	<item quantity=”few”>You have %d messages waiting!</item>
	<item quantity=”many”>%1$d messages in your inbox! %2$s, youshould really login here more often!</item>
	<item quantity=”other”>%1$d messages in your inbox! %2$s, youshould really login here more often!</item>
</plurals>
<string-array name=”default_categories”>
	<item>Work</item>
	<item>Personal</item>
	<item>Private</item>
	<item>Spam</item>
	<item>Trash</item>
	<item>Draft</item>
</string-array>

格式化

Strings.xml可以写格式化字符串,此处%s可用String来填补

例子

    public void showWelcomeMessage(String name) {
        ((TextView) findViewById(R.id.welcome_message_field)).
        setText(getString(R.string.personal_welcome_message, name));
}

复数

示例代码

public void showInboxCountMessage(int inboxCount, String name) {
        Resources res = getResources();
        String inboxCountMessage = res.
                getQuantityString(R.plurals.inbox_message_count, inboxCount,
                        name);
        ((TextView) findViewById(R.id.inbox_count_field)).
                setText(inboxCountMessage);
}

这种方式主要用来处理不同数量时显示不同的字符串,getQuantityString的第二个参数就是数量,android里的数量有几种zero,one,two,few,many

可参考http://blog.youkuaiyun.com/ff313976/article/details/7439676

 

字符串数组

可以把一些相关的字符串放入一个字符串数组内,示例代码如下

public void displayCategories() {
        ListView listView = (ListView) findViewById(R.id.category_list);
        Resources res = getResources();
        String[] categories = res.getStringArray(R.array.default_categories);
        ArrayAdapter<String> categoriesadapter = new
                ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1,
                categories);
        listView.setAdapter(categoriesadapter);
}

参考文献

android pragramming push the limits

### Python F 字符串用法 F 字符串(也称为格式化字符串字面量)是在 Python 3.6 版本引入的一种新特性。这种字符串允许嵌入表达式,这些表达式的计算结果会替换到最终输出中。 #### 创建 F 字符串 要创建一个 f-string,在字符串前加一个小写的 `f` 或大写的 `F` 即可: ```python name = "Alice" age = 30 formatted_string = f"My name is {name} and I am {age} years old." print(formatted_string) ``` 上述代码将会打印 `"My name is Alice and I am 30 years old."`[^1] #### 表达式支持 可以在花括号内放置任何合法的 Python 表达式而不仅仅是变量名称: ```python a, b = 5, 10 result = f"The sum of {a} plus {b} equals to {a+b}." print(result) ``` 这段程序将显示如下消息:"The sum of 5 plus 10 equals to 15." #### 调用方法和属性访问 还可以直接调用对象的方法或访问其属性作为表达式的一部分: ```python text = "hello world!" capitalized_text = f"{text.capitalize()}" print(capitalized_text) ``` 这会产生输出:“Hello world!” #### 使用转义字符处理特殊情况 如果需要在 f-string 中包含实际的大括号,则可以使用双层大括号来进行转义: ```python example = f"{{This will be printed as a single brace}}" print(example) ``` 该语句的结果将是:`{This will be printed as a single brace}`. #### 结合其他功能 当然也可以与其他高级特性一起工作,比如列表解析、生成器表达式等复杂结构: ```python numbers = [n**2 for n in range(5)] output = ", ".join(f"{i}:{num}" for i, num in enumerate(numbers)) print(output) ``` 以上脚本会给出这样的输出序列:`0:0, 1:1, 2:4, 3:9, 4:16`.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值