About substring char [] sharing, substringchar sharing
We know that if a large String object obtains a substring from it, the char [] of the jdk substring is the char [] of the original substring by default. that is, the char [] of the substring is part of the char [] of the original string,
This saves a lot of space when multiple substrings of an original string exist.
But it is also because of sharing. If a large original string is no longer needed after obtaining a small substring, but it cannot be released because it shares char, in many cases, the opposite is true.
Results, or even performance problems:
See: https://code.google.com/p/mybatis/issues/detail? Id = 760
To solve this problem, in JDK 6, we can only use the new substring after obtaining the substring, so that the char [] of the original string is no longer referenced and thus can be quickly released:
String src = "abcdefghijklmnopqrstuvwxyz1234567890-=";String sub = src.substring(1,4);sub = new String(sub);
...................................
Use sub
The code in this way is obscure and difficult to query. JDK 7 removes sharing, and JDK 7 optimizes copying by using the simd command of the cpu. In most cases, the performance of jdk7 strings is better than that of jdk6:
1950 public String substring(int beginIndex, int endIndex) { 1951 if (beginIndex < 0) { 1952 throw new StringIndexOutOfBoundsException(beginIndex); 1953 } 1954 if (endIndex > count) { 1955 throw new StringIndexOutOfBoundsException(endIndex); 1956 } 1957 if (beginIndex > endIndex) { 1958 throw new StringIndexOutOfBoundsException(endIndex - beginIndex); 1959 } 1960 return ((beginIndex == 0) && (endIndex == count)) ? this : 1961 new String(offset + beginIndex, endIndex - beginIndex, value); 1962 }
Compile the substring (char s [], char sub []) function to find the subscript position of the string sub that appears for the first time in string s. If not found, return-1
# Include <iostream. h>
Int Strfind (char * s, char * t)
{
Int index, lengths = 0, lengtht = 0, I, j;
Char * Position;
While (s [lengths]! = 0) // obtain the length of string s
{
Lengths ++;
}
While (t [lengtht]! = 0)
{
Lengtht ++;
}
For (I = 0; I <lengths-lengtht; I ++) // cycle to the length of the source string minus the length of the target string.
{
Index = 0;
While (s [I + index] = t [index] & index <lengtht)
{
Index ++;
}
If (index = lengtht)
{
// Position = new char;
// * Position = I;
// Return Position;
Return I; // the position where I is returned.
}
}
// Position = NULL;
Return-1; // cannot be found
}
Int main ()
{
Char str1 [500];
Char str2 [500];
Cin> str1;
Cin> str2;
// Char * Position = Strfind (str1, str2 );
// Cout <* Position <endl;
Cout <Strfind (str1, str2) <endl;
Return 0;
}
How to use C language to implement a string truncation function prototype: char * subString (char * str, int star, int len );
Char * subString (char * str, int star, int len );
This prototype is not good. Where can I store the returned substring?
I. If malloc is used for allocation, the function caller forgets free, and the memory will leak.
Ii. If it is placed in the source string, the ring source string will be broken
Char * subString (char * dst, char * src, int star, int len );