在linux系统中OC编程如何实现字符串连接
2个回答
展开全部
由于Xcode对中文支持良好,所以在开发过程中经常直接使用中文字符串。
不过苹果推荐多语言化,需要为中文字符串添加个NSLocalizedString宏。
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''''
Localization The Objective-C Code
@"..." --> NSLocalizedString(@"...", nil)
Jason Lee 2012-03-01
'''
import os, sys
import re
import codecs
targetPattern = pile('@"[^"]+"')
global newFile, newFilePointer
def isChineseCharacter(ch):
return0x4e00 <= ord(ch) <= 0x9fa5
def hasChineseCharacter(str):
for char in str:
if isChineseCharacter(char):
returnTrue
returnFalse
def buildNewString(oldStr):
newStrPrefix = 'NSLocalizedString('
newStrSuffix = ', nil)'
newStr = newStrPrefix + oldStr + newStrSuffix
return newStr
def processLine(line):
global newFile, newFilePointer
matchResult = targetPattern.findall(line)
for result in matchResult:
if hasChineseCharacter(result):
#print result, buildNewString(result)
p = pile(result)
line = p.sub(buildNewString(result), line)
newFilePointer.write(line)
def processFile(filename):
#Xcode file is saved with utf-8
global newFile, newFilePointer
newFile = 'Replaced.' + filename
newFilePointer = codecs.open(newFile, 'wb', 'utf-8')
fp = codecs.open(filename, 'rb', 'utf-8')
for line in fp:
processLine(line)
fp.close()
newFilePointer.close()
oldFile = 'Old.' + filename
os.system('mv ' + filename + ' ' + oldFile)
os.system('mv ' + newFile + ' ' + filename)
#os.system('rm -f ' + oldFile)
if __name__ == "__main__":
if len(sys.argv) > 1:
output = os.popen('ls ' + sys.argv[1]).read()
filelist = re.split('\n', output)
filelist = filelist[:-1]
#print filelist
print'Localizing...'
for file in filelist:
if os.path.exists(file):
try:
#print 'Processing File :', file
processFile(file)
except Exception as e:
print e
print'Localization Done.'
之后需要做的事情参考:
代码没用经过严格验证,请慎用。起码,没有检查该字符串是否已经加了NSLocalizedString宏。
不过苹果推荐多语言化,需要为中文字符串添加个NSLocalizedString宏。
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''''
Localization The Objective-C Code
@"..." --> NSLocalizedString(@"...", nil)
Jason Lee 2012-03-01
'''
import os, sys
import re
import codecs
targetPattern = pile('@"[^"]+"')
global newFile, newFilePointer
def isChineseCharacter(ch):
return0x4e00 <= ord(ch) <= 0x9fa5
def hasChineseCharacter(str):
for char in str:
if isChineseCharacter(char):
returnTrue
returnFalse
def buildNewString(oldStr):
newStrPrefix = 'NSLocalizedString('
newStrSuffix = ', nil)'
newStr = newStrPrefix + oldStr + newStrSuffix
return newStr
def processLine(line):
global newFile, newFilePointer
matchResult = targetPattern.findall(line)
for result in matchResult:
if hasChineseCharacter(result):
#print result, buildNewString(result)
p = pile(result)
line = p.sub(buildNewString(result), line)
newFilePointer.write(line)
def processFile(filename):
#Xcode file is saved with utf-8
global newFile, newFilePointer
newFile = 'Replaced.' + filename
newFilePointer = codecs.open(newFile, 'wb', 'utf-8')
fp = codecs.open(filename, 'rb', 'utf-8')
for line in fp:
processLine(line)
fp.close()
newFilePointer.close()
oldFile = 'Old.' + filename
os.system('mv ' + filename + ' ' + oldFile)
os.system('mv ' + newFile + ' ' + filename)
#os.system('rm -f ' + oldFile)
if __name__ == "__main__":
if len(sys.argv) > 1:
output = os.popen('ls ' + sys.argv[1]).read()
filelist = re.split('\n', output)
filelist = filelist[:-1]
#print filelist
print'Localizing...'
for file in filelist:
if os.path.exists(file):
try:
#print 'Processing File :', file
processFile(file)
except Exception as e:
print e
print'Localization Done.'
之后需要做的事情参考:
代码没用经过严格验证,请慎用。起码,没有检查该字符串是否已经加了NSLocalizedString宏。
展开全部
如果想要在变量后面添加一个字符,可以用一下方法:
$value1=home
$value2=${value1}"="
echo $value2
把要添加的字符串变量添加{},并且需要把$放到外面。
这样输出的结果是:home=,也就是说连接成功。
又如:
[root@localhost sh]# var1=/etc/
[root@localhost sh]# var2=yum.repos.d/
[root@localhost sh]# var3=${var1}${var2}
[root@localhost sh]# echo $var3
/etc/yum.repos.d/
$value1=home
$value2=${value1}"="
echo $value2
把要添加的字符串变量添加{},并且需要把$放到外面。
这样输出的结果是:home=,也就是说连接成功。
又如:
[root@localhost sh]# var1=/etc/
[root@localhost sh]# var2=yum.repos.d/
[root@localhost sh]# var3=${var1}${var2}
[root@localhost sh]# echo $var3
/etc/yum.repos.d/
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询