replaceText

Hi all,
I have this code


inputfield.replaceText(inputfield.caretIndex,inputfield.caretIndex,e.text.toUpperCase( ));

I read a reference . ““about replaceText””
I do not know how ; caretIndex can be a value for begin of string and at the same time the end of string .

Thanks for reading .

This code doesn’t replace anything, it adds a letter probably entered from keyboard in the place where the caret is at the moment… May it be you wanted to get selection boundaries?

var tf:TextField = new TextField();
tf.width = 200;
tf.height = 20;
tf.border = true;
tf.type = 'input';
addChild(tf);
stage.addEventListener(KeyboardEvent.KEY_DOWN, handleTextInput);
function handleTextInput(evt:KeyboardEvent):void {
	if(evt.keyCode == Keyboard.ENTER){
		var startSelection:int = tf.selectionBeginIndex;
		var endSelection:int = tf.selectionEndIndex;
		var totalToReplace:int = endSelection - startSelection;
		var repArray:Array = new Array(totalToReplace);
		repArray.forEach(toAsterisk);
		tf.replaceSelectedText(repArray.join(''));
	}
}
function toAsterisk(o:*, i:int, a:Array):void {
	a* = '*';
}

Something like this?

Not even close .
I mean on method replaceText () .
So the reference say this :

replaceText(beginIndex:int, endIndex:int, newText:String):void
Replaces the range of characters that the beginIndex and endIndex parameters specify with the contents of the newText parameter.

and Author code is above ; so I am confused how can same name represent beginIndex and endIndex .

The same way as you can do array.splice(1,0,‘new array element’) // this won’t delete any elements from array, but add one more at the second position. Or ‘abcd’.replace(’’,‘f’) //will add ‘f’ to the beginning of the string etc…
If it says beginIndex should be integer and endIndex should to be integer it doesn’t mean they have to be different, they may be the same as wel… =\

It is not the same thing .

Thanks for trying to help .

caretIndex can be a value for begin of string and at the same time the end of string.

Ok, simple:

var a:String = '';
trace(a.charAt(0)); // first char
trace(a.charAt(a.length-1)); // last char
// them both point to the same char

If I ware to write this function I’d do it like this:

var test_str:String = 'abcdefghjklmopq';
function myReplaceText(sorce:String, a:int, b:int, s:String=null):String {
	if(s == null) return sorce;
	var sub1:String = sorce.slice(0, Math.min(a, b));
	var sub2:String = sorce.slice(Math.max(a, b), sorce.length);
	return sub1 + s + sub2;
}
trace(myReplaceText(test_str, 2, 5, '******'));
trace(myReplaceText(test_str, 4, 4, '4444'));