# replaceText

**URL:** <https://forum.kirupa.com/t/replacetext/260735>\
**Category:** Uncategorized\
**Created:** [May 19, 2008, 12:28pm UTC](https://forum.kirupa.com/t/replacetext/260735 "2008-05-19T12:28:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kv79](https://avatars.discourse-cdn.com/v4/letter/k/47e85d/32.png) [@kv79](https://forum.kirupa.com/u/kv79)\
**Post date:** [May 19, 2008, 12:28pm UTC](https://forum.kirupa.com/t/replacetext/260735/1 "2008-05-19T12:28:27Z")

</div>

Hi all,  
I have this code

```auto

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 .

---

<div class="post-metadata">

**Author:** ![wvxvw](https://avatars.discourse-cdn.com/v4/letter/w/a9a28c/32.png) [@wvxvw](https://forum.kirupa.com/u/wvxvw)\
**Post date:** [May 19, 2008, 2:42pm UTC](https://forum.kirupa.com/t/replacetext/260735/2 "2008-05-19T14:42:21Z")

</div>

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?

```auto
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?

---

<div class="post-metadata">

**Author:** ![kv79](https://avatars.discourse-cdn.com/v4/letter/k/47e85d/32.png) [@kv79](https://forum.kirupa.com/u/kv79)\
**Post date:** [May 19, 2008, 4:37pm UTC](https://forum.kirupa.com/t/replacetext/260735/3 "2008-05-19T16:37:55Z")

</div>

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 .

---

<div class="post-metadata">

**Author:** ![wvxvw](https://avatars.discourse-cdn.com/v4/letter/w/a9a28c/32.png) [@wvxvw](https://forum.kirupa.com/u/wvxvw)\
**Post date:** [May 19, 2008, 6:33pm UTC](https://forum.kirupa.com/t/replacetext/260735/4 "2008-05-19T18:33:32Z")

</div>

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… =\

---

<div class="post-metadata">

**Author:** ![kv79](https://avatars.discourse-cdn.com/v4/letter/k/47e85d/32.png) [@kv79](https://forum.kirupa.com/u/kv79)\
**Post date:** [May 20, 2008, 11:45am UTC](https://forum.kirupa.com/t/replacetext/260735/5 "2008-05-20T11:45:28Z")

</div>

It is not the same thing .

Thanks for trying to help .

---

<div class="post-metadata">

**Author:** ![wvxvw](https://avatars.discourse-cdn.com/v4/letter/w/a9a28c/32.png) [@wvxvw](https://forum.kirupa.com/u/wvxvw)\
**Post date:** [May 20, 2008, 2:11pm UTC](https://forum.kirupa.com/t/replacetext/260735/6 "2008-05-20T14:11:23Z")

</div>

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

Ok, simple:

```auto
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:

```auto
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'));

```
