php处理大量字符串似乎很慢,无论如何我可以提高它的速度吗?我试图写的代码会将图像变成一串RGB值供以后使用,就像这样
$string = "255:255:253#12:12:23#33:34:24"/*an output of a $SIZE = 3 image*/
Run Code Online (Sandbox Code Playgroud)
问题是当$ SIZE大到256时,产生字符串需要1秒钟
$r = "";
$g = "";
$b = "";
for($y = 0; $y <= $SIZE-1; $y++){
for($x = 0; $x <= $SIZE-1; $x++){
{$r .= $arr2[$y][$x]["R"].":";}
{$g .= $arr2[$y][$x]["G"].":";}
{$b .= $arr2[$y][$x]["B"].":";}
}
}
$r = rtrim($r, ":");
$g = rtrim($g, ":");
$b = rtrim($b, ":");
$str_a .= $r."#".$g."#".$b;
Run Code Online (Sandbox Code Playgroud) 有没有办法检查字符串是否是有效的 UUID?
在我的情况下,我有文件列表,其中一些文件名是由 生成的 UUID UUID.randomUUID().toString()
,其余的只是普通.jpg
文件。
我知道通过使用UUID.fromString(filename)
和捕获IllegalArgumentException
抛出,我将能够检查是否filename
是格式正确的 UUID。但考虑到文件列表将包含大量文件,这似乎非常昂贵。有没有一种方法可以让我在不引发异常的情况下进行检查?
为什么这样:
package com.example;
import com.example.Foo.Bar.Baz;
import java.io.Serializable; // I did import Serializable...
public class Foo implements Serializable {
public final Bar bar;
public Foo(Bar bar) {
this.bar = bar == null ? new Bar(Baz.ONE) : bar;
}
public static class Bar implements Serializable { // this is line 15, where the compiler error is pointing
public enum Baz {
ONE
}
public final Baz baz;
public Bar(Baz baz) {
this.baz = baz;
}
}
}
Run Code Online (Sandbox Code Playgroud)
给我这个:
[ERROR] <path to file>/Foo.java:[15,44] …
Run Code Online (Sandbox Code Playgroud) 我有一个字符串,我想分成一个数组:
SEQUENCE? 1A?2B?3C
我尝试了以下正则表达式:
((.*\s)|([\x{2192}]*))
1. \x{2192} is the arrow mark
2. There is a space after the colon, I used that as a reference for matching the first part
Run Code Online (Sandbox Code Playgroud)
它适用于测试人员(OSX中的模式)
但它将字符串拆分为:
[, , 1, A, , 2, B, , 3, C]
如何实现以下目标?:
[1A,2B,3C]
这是测试代码:
String str = "SEQUENCE? 1A?2B?3C"; //Note that there's an extra space after the colon
System.out.println(Arrays.toString(str.split("(.*\\s)|([\\x{2192}]*)")));
Run Code Online (Sandbox Code Playgroud)