Tag "Strings" (9)
Java: Get quoted string regex java
Get quoted string regex java ... Reveal Code
String REGEX = "\"([^\"]*)\"";
String sentence = "World \"Hello\" World!";
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(sentence);
if(matcher.find()){
String result = matcher.group(1);
System.out.println(result);
} else {
System.out.println("Nothing");
}
C#: StringExtentions
StringExtentions ... Reveal Code
public static class StringExtentions
{
public static String Format(this String str, params Object[] args)
{
return String.Format(str, args);
}
public static String Format(this String str, IFormatProvider provider, params Object[] args)
{
return String.Format(provider, str, args);
}
}
C++: Get Between the String
returns the string between two strings ... Reveal Code
string GetBetween(string target, string left, string right)
{
size_t begin = target.find(left);
size_t end = target.find(right);
if(begin==string::npos || end==string::npos) return "";
begin += left.size();
return target.substr(begin, end - begin);
}
JavaScript: Email validation
Validation of the entered email using regex ... Reveal Code
function IsEmail(email) {
var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return regex.test(email);
}
PHP: Make a clickable text
Make a clickable text: replace all the URLs with tag ... Reveal Code
function makeClickable($text) {
$text = preg_replace("/(^|[\n ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is", "$1$2$3", $text);
$text = preg_replace("/(^|[\n ])([\w]*?)((www|ftp)\.[^ \,\"\t\n\r<]*)/is", "$1$2$3", $text);
$text = preg_replace("/(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", "$1$2@$3", $text);
return($text);
}
Delphi: Bytes formatting
Formatting byte values to a more readable values ... Reveal Code
function FormatByteSize(const bytes: Longint): string;
const
B = 1; //byte
KB = 1024 * B; //kilobyte
MB = 1024 * KB; //megabyte
GB = 1024 * MB; //gigabyte
begin
if bytes > GB then
result := FormatFloat('#.## Gb', bytes / GB)
else if bytes > MB then
result := FormatFloat('#.## Mb', bytes / MB)
else if bytes > KB then
result := FormatFloat('#.## Kb', bytes / KB)
else
result := FormatFloat('#.## bytes', bytes);
end;
Delphi: Замена подстроки в сторке
Замена всех подстрок в данной строке. Функция была использована от Delphi 7 до Delphi XE3. ... Reveal Code
function ReplaceSub(str, sub1, sub2: string): string;
var
aPos: Integer;
rslt: string;
begin
aPos := pos(sub1, str);
rslt := '';
while (aPos <> 0) do
begin
rslt := rslt + Copy(str, 1, aPos - 1) + sub2;
Delete(str, 1, aPos + Length(sub1) - 1);
aPos := pos(sub1, str);
end;
result := rslt + str;
end;
procedure TForm1.btn1Click(Sender: TObject);
var
my_str: string;
begin
my_str := 'My cool string is very cool!';
ShowMessage(ReplaceSub(my_str, 'cool', 'awesome'));
end;
PHP: Bytes formatting
Formatting byte values to a more readable values ... Reveal Code
function byte_format($num) {
if ($num >= 1000000000000) {
$num = round($num / 1099511627776, 1);
$unit = "Tb";
} elseif ($num >= 1000000000) {
$num = round($num / 1073741824, 1);
$unit = "Gb";
} elseif ($num >= 1000000) {
$num = round($num / 1048576, 1);
$unit = "Mb";
} elseif ($num >= 1000) {
$num = round($num / 1024, 1);
$unit = "Kb";
} else {
$unit = "Bytes";
return number_format($num) . ' ' . $unit;
} return number_format($num, 1) . ' ' . $unit;
}
PHP: Generate a random password
Generate a random password with an arbitrary length ... Reveal Code
function generatePassword($length = 8) {
$chars = 'abdefhiknrstyzABDEFGHKNQRSTYZ23456789';
$numChars = strlen($chars);
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= substr($chars, rand(1, $numChars) - 1, 1);
}
return $string;
}