例如,假设我有以下代码:
String s1 = "foobar";
s1 = s1.substring(3);
Run Code Online (Sandbox Code Playgroud)
该代码的效率是否低于:
String s1 = "foobar";
s1 = s1.substring(3, 6);
Run Code Online (Sandbox Code Playgroud)
我认为两个参数方法在性能方面会更有效,因为单个参数方法使用循环来遍历索引,直到达到长度.这意味着JVM必须调用该length()方法以找出何时停止循环.
但是这两个参数方法只循环直到达到最后一个索引号.
任何人都可以证实或否认我的假设吗?
编辑:我真的不明白源代码中的这个代码(last return语句),但这里是java String类源:
public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > count) {
throw new StringIndexOutOfBoundsException(endIndex);
}
if (beginIndex > endIndex) {
}
return ((beginIndex == 0) && (endIndex == count)) ? this : // I don't understand this part
new String(offset + beginIndex, endIndex - beginIndex, …Run Code Online (Sandbox Code Playgroud) 我一直坚持这个问题一段时间了.非常感谢帮助.
我有一个4位数字从文件中读入,我需要取内部2位数.我认为以字符串形式读取数字是一个好主意,然后在子字符串中取两个中间数字并使用numval函数将它们转换回来.不幸的是,我无法弄清楚如何获得里面的两个字符.
防爆.我有5465号,我想要46号.
因此,这是一个简单的代码,试图在许多文件中找到短语("各种资产")的出现频率.
import codecs
import glob
import os.path
filelocation = "C:\\Users\\Shoi\\Desktop\\mark project\\BITs\\*.txt"
for filepath in glob.glob(filelocation): # for each file
FILE = codecs.open(filepath, 'r', encoding="utf-8")
if ("every kind of asset" in FILE.read().lower()):
print ("Found in " + os.path.basename(filepath))
freq = FILE.read().lower().count("every kind of asset")
print(freq)
else:
print ("not found in " + os.path.basename(filepath))
Run Code Online (Sandbox Code Playgroud)
但是,即使在某些文件中找到该短语(打印出"找到"文件) - 计数功能也会返回并始终打印0.
此代码仅搜索单个短语.当我遍历一个短语列表,搜索所有文件中的每个短语时 - count函数为某些短语返回完全正确的频率结果,但对其他短语返回0,即使该短语存在于文件中并且它打印"found"
请帮忙.
Powershell中是否有一个函数可以根据给定的格式化掩码来减少值?
让我们使用例如 $test = "Value 14, Code 57"
Cut-Substring("Value $val, Code $code",$test)
Run Code Online (Sandbox Code Playgroud)
结果我想收到$val = 14和$code = 57.
如果没有,是否有更强大的工具允许访问给定标签旁边的字段?
如何在两个已知字符串之间获取字符串?例如:
<id> 100 </id>
Run Code Online (Sandbox Code Playgroud)
我想从这个字符串中获得100.
我试过这段代码,但它不起作用.该NSLog方法是:
<id>100
Run Code Online (Sandbox Code Playgroud)
码:
NSString *serverOutput = @"<id>100</id>";
NSRange start = [serverOutput rangeOfString:@"<id>"];
NSRange end = [serverOutput rangeOfString:@"</id>"];
NSLog(@"%@",[serverOutput substringWithRange:NSMakeRange(start.location, end.location)]);
Run Code Online (Sandbox Code Playgroud) 我有以下声明
model.Activities = model.SelectedActivities.Aggregate(model.Activities, (current, activityId) => current + string.Format("({0}, {1}, {2}, null),", activityId, userId, DateTime.Now));
Run Code Online (Sandbox Code Playgroud)
产生以下内容:
(1, 1, 05/04/2015 05:09:39, null),(2, 1, 05/04/2015 05:09:39, null),(3, 1, 05/04/2015 05:09:39, null),(5, 1, 05/04/2015 05:09:39, null),(8, 1, 05/04/2015 05:09:39, null),
Run Code Online (Sandbox Code Playgroud)
你可以在最后看到有一个逗号,我试图通过执行以下操作来删除它:
var t = model.Activities.Substring(1, model.Activities.Length - 1);
Run Code Online (Sandbox Code Playgroud)
但是这句话似乎没有删除最后一个逗号而是保持不变,我在这里做错了什么?
我有这个字符串:
"<C (programming language)> <C++ (programming language)> <Programming Languages> <Computer Programming> "
Run Code Online (Sandbox Code Playgroud)
我想获得一个子串列表,如下所示:
['<C (programming language)>','<C++ (programming language)>','<Programming Languages>','<Computer Programming>']
Run Code Online (Sandbox Code Playgroud)
我尝试使用re库python但没有成功
我已经声明了一个字符串,我想从字符串中获取第二个单词.例如.
string str = "Hello world";
Run Code Online (Sandbox Code Playgroud)
我想从str得到"世界"这个词.
我有一个FreeMarker变量,${string}可以是任何长度.我怎样才能获得最后四个字符?
例如:
string ="326235253235235";
我要显示:"5235";
在Perl 5.20中,我需要在子字符串上运行正则表达式而不将其复制到新字符串中.所以有些相当于$str[$to]C语言.
原因是它在循环中执行,如果它每次都复制字符串,那么结果代码就是O(n^2)没有复制的情况O(n).因此,如果输入2MB字符串,则无法使用.
或者,我将欢迎有关如何重写代码的建议:我需要使用查找表进行搜索和替换.
示例输入
$str: "abcde"
$tbl: {ab=>"xy", bc=>"rq", e=>"a"}
$reg: qr/ab|bc|e/
expected output: xycda
Run Code Online (Sandbox Code Playgroud)
这是我当前的代码,对于短字符串运行良好,但它不适用于大字符串:
#translate $str: if $reg match found, replace the match with a value in $tbl hash that corresponds to the match
sub internalEncode {
my ($str, $tbl, $reg) = @_;
my $res="";
my $prevTo = 0;
my $to = 0;
#the substr($str,$to) makes it slow; in C with 0 terminated strings
#I would need to write here something …Run Code Online (Sandbox Code Playgroud) substring ×10
string ×7
c# ×2
java ×2
python ×2
cobol ×1
freemarker ×1
ios ×1
jvm ×1
list ×1
objective-c ×1
performance ×1
perl ×1
powershell ×1
python-2.7 ×1
replace ×1
swift ×1