The detector generation is the key step of negative selection. Current detector generation algorithms have holes area and redundancy detector problems. A negative selection algorithm with the variable length detector is proposed in this paper. This algorithm can not only remove the holes, but also decrease redundancy detectors by the corresponding detector optimization algorithm. Therefore, both the detector generation efficiency and the detecting efficiency are improved well. This algorithm is analyzed in this paper and verified by experiments. The experimental results prove that this algorithm is better than the traditional negative selection algorithms and the negative selection algorithm with the r-adjustable detector.