Java and SQL implementations take values between two characters
- 2021-09-11 20:25:39
- OfStack
Java
String str = "abcdefg";
String result = str.substring(str.indexOf(">")+1, str.lastIndexOf("<"));
StringUtils.substringBefore( " abcdec " , " c " );
The result is: ab here is based on the first "c".
StringUtils.substringBeforeLast( " abcdec " , " c " )
The result is: abcde is based on the penultimate "c".
SQL
Intercept "*" and previous characters
In sql, you can intercept special characters by using the following function:
substr ( str,instr(str,'*',1)+1)
Among them, substr function and instr function are used.
1. substr (string, start_position, [length]) Find a substring and return a string
The first parameter represents the string to be truncated, the second represents the starting position, and the third represents the length of the truncation, which can be omitted.
2. instr (string, subString, position, ocurrence) Find the position of the string
The first parameter represents the source string, the second is the character to be found, the third is the starting position to be found, and the fourth is the character to be found.
Take that information between two character in a field
1. As follows
substring(a.specifications,charindex(':',a.specifications,charindex(';',specifications))+1,1)
SQL error report
FUNCTION db.charindex does not exist
2. Use the substring_index syntax
Usage rules:
substring_index ("String of useful part to be intercepted", "Character by which data is intercepted", position of intercepted character N)
Specific to intercept the N comma before the part of the characters, in the string with comma as the index, get the characters of different index bits.
N can be negative and represents the string after the penultimate N index character. (If you have a minus sign, you can look at the whole character upside down, and it is still the part before the N character.)
Specific examples:
First assume that the string to be intercepted is "192; 168; 8; 203", where the interception is based on the semicolon: ";"
The results are as follows:
Take the string before the first comma:
SELECT SUBSTRING_INDEX( ' 192;168;8;203',';',1);
==> The result is: 192
Take the part after the last comma:
SELECT SUBSTRING_INDEX( ' 192;168;8,203',';',-1);
==> The result is: 203
Take the part of the string before the second comma and after the last comma
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX( ' 192;168;8;203',';',2),';',-1);
==> The result is: 168
Take the part of the string after the penultimate comma, and then go to the part before the first comma in this part:
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX( ' 192;168;8,203',';',-2),';',1);
==> The result is: 8
java Two strings intersect
Take the intersection of two strings
public static void checkCommom(String str1, String str2) {
HashSet<String> result = new HashSet<String>();
int length1 = str1.length();
int length2 = str2.length();
for (int i = 0; i < length1; i++) {
for (int j = 0; j < length2; j++) {
String char1 = str1.charAt(i) + "";
String char2 = str2.charAt(j) + "";
if (char1.equals(char2))
{
result.add(char1);
}
}
}
Iterator<String> it = result.iterator();
while (it.hasNext()) {
String value = it.next();
System.out.print(value);
}
}