insert text in the middle of string in flex 3 - apache-flex

can you please help me with this issue the String class does not have insert method it has only replace :( .
what I need is:
- if I have string "I stackoverflow"
- I need to insert "love " at index 2 to have "I love stackoverflow"
so what I need is insertAt(index, String)
thanks

You can build one of your own. Split the string and concatenate all characters before the index position with the characters of the string you want to insert and with the characters after the index.
eg:
String s = "I stackoverflow";
int index = s.indexOf(" ");
String toInsert = "love ";
String mys = s.substring(0, index) + toInsert + s.substring(index, s.length);

var s:String = "I StackOverflow";
var t:String = s.split(" ").join(" love ");

Related

How do you store a certain part of a string into a variable?

How do you store a certain part of a string into a variable?
For example:
x = myString // - But store the 9th character into a variable
Try this,
x = string_char_at(myString , 9);
This gets a single character from a string:
var x = string_char_at("This is my string", 4); //X == "s"
And you can use the string_copy function to copy parts of a string;
var x = string_copy("This is my string", 8, 2); //X == "my"

separating decimal numbers with commas in asp.net eg:1,23,456

I tried this format, got output as 1,234,567,890.00
but I need in this format
1,23,567
decimal unitPrice = 1234567890.00m;
TextBox1.Text = Convert.ToDecimal(unitPrice).ToString("##,#0.00");
I guess you can do this by creating a custom number format info for your needs...example is here for your reference -
int yourNumber = 111234;
NumberFormatInfo nfo = new NumberFormatInfo();
nfo.CurrencyGroupSeparator = ",";
nfo.NumberGroupSizes = new int[] { 3, 2 };
string str = yourNumber.ToString("N0", nfo);
Response.Write("answer is : " + str);

Sqlite Full Text Search Query Puzzle

I have data in format yyyy-MM-dd hh:mm:ss. It is stored as text.
I want a query that matches a given day(yyyy-MM-dd).
E.g
Select * from test where test MATCH '2014-03-30*'
When i try that, it returns all data excluding that of March 2014.
If i try
Select * from test where test MATCH '2014-04-30*'
It returns all data excluding that of April 2014.
I am puzzled!...i am getting the opposite of what i want!
Any reason for the strange behavior?
This is my full code....testing for date pattern
public List <Transactions> GetTransactionsVirtual(String token)
{
List<Transactions> trans = new ArrayList<Transactions>();
SQLiteDatabase db;
String sql= " Select " + MESSAGE + "," + TDATE + ","+ SERVICE_PROVIDER +
" from " + TABLE_NAME_VIRTUAL + " Where "+ TABLE_NAME_VIRTUAL + " MATCH " + "?" +
" Order by " + TDATE + " DESC";
//check entered string...if date string strip it ..
String pattern="^(19|20)\\d\\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$";
String tokenedit=null;
String newtoken=null;
if (token.matches(pattern))
{
tokenedit= token.replace("-", " ");
Log.e("testtoken", tokenedit);
newtoken = tokenedit+"*" ;
}else
{
newtoken= token+ "*";
}
String [] args= new String[]{newtoken};
Log.e("sqlamatch", sql);
db= this.getReadableDatabase();
Cursor c = db.rawQuery(sql, args);
if(c.moveToFirst())
{
do{
Transactions t=new Transactions();
t.setTransactiondate(c.getString(c.getColumnIndex(TDATE)));
t.setMessage(c.getString(c.getColumnIndex(MESSAGE)));
t.setServiceprovider(c.getString(c.getColumnIndex(SERVICE_PROVIDER)));
//Log.e("msg",t.getMessage().toString());
trans.add(t);
}while(c.moveToNext());
}
return trans;
}
The full-text search mechanism is designed for searching words inside texts.
With the default tokenizer, the three fields of a date (yyyy, MM, dd) are parsed as single, independent words, and the delimiters are ignored.
An FTS query like 2014-03-30* searches for documents (=records) that
contain the word 2014, and
do not contain the word 03, and
do not contain any word beginning with 30.
You need to do a phrase search:
SELECT * FROM test WHERE test MATCH '"2014 03 30"'
If all you data has a fixed format, you should not use an FTS table in the first place.

asp.net Substring from specific string to the end of the word

I have a multiline textbox that user may type whatever he wants to for example,
"Hello my name is #Konstantinos and i am 20 #years old"
Now i want to place a button when is pressed the output will be #Konstantinos and #years -
Is that something that can be done using substring or any other idea?
Thank you in advance
If all that you want is HashTags(#) from the entire string, you can perform simple .Split() and Linq. Try this:
C#
string a = "Hello my name is #Konstantinos and i am 20 #years old";
var data = a.Split(' ').Where(s => s.StartsWith("#")).ToList();
VB
Dim a As String = "Hello my name is #Konstantinos and i am 20 #years old"
Dim data = a.Split(" ").Where(Function(s) s.StartsWith("#")).ToList()
Using regex will give you more flexibility.
You can define a pattern to search for strings starting with #.
.Net regex cheat sheet
Dim searchPattern = "#(\S+)" '\S - Matches any nonwhite space character
Dim searchString = "Hello my name is #Konstantinos and i am 20 #years old"
For Each match As Match In Regex.Matches(searchString, searchPattern, RegexOptions.Compiled)
Console.WriteLine(match.Value)
Next
Console.Read()
This will work . Try this..
string str = "Hello my name is #Konstantinos and i am 20 #years old asldkfjklsd #kumod";
int i=0;
int k = 0;
while ((i = str.IndexOf('#', i)) != -1)
{
string strOutput = str.Substring(i);
k = strOutput.IndexOf(' ');
if (k != -1)
{
Console.WriteLine(strOutput.Substring(0, k));
}
else
{
Console.WriteLine(strOutput);
}
i++;
}

string replace not working at all

I am reading a text file and I need to replace a few areas with new text that I marked with {0}, {1}, {2}, {3}, {4}, {5}, {6}.
So I load in the text, save it to a string variable, and then use String.Replace but it's not working.
For i As Integer = 0 To 6
fileText.Replace("{" & i & "}", DisplayStudentData(i))
Next
And DisplayStudentData looks like this:
Protected Function DisplayStudentData(ByVal itemNumber As Integer) As String
Dim dsItem As String = ""
If itemNumber <> -1 Then
Select Case itemNumber
Case 0
dsItem = "testFirstName"
Case 1
dsItem = "testTitle"
Case 2
dsItem = "testClass"
Case 3
dsItem = "testTeacher"
Case 4
dsItem = "testDept"
Case 5
dsItem = "testEmail"
Case 6
dsItem = "testPhone"
End Select
End If
Return dsItem
End Function
It seems like the above should work, but it doesn't.
Any help would be greatly appreciated.
Thanks!
Ok I figured it out...
I put all the data items into an array, then did this after loading the text file:
fileText = String.Format(fileText, dArr(0), dArr(1), dArr(2), dArr(3), dArr(4), dArr(5), dArr(6))
Is this a good way of doing it?
fileText = fileText.Replace("{" & i & "}", DisplayStudentData(i))
Replace returns a new string. It does not modify the string from which it was called.
You could also do this:
fileText = String.Format(fileText, Enumerable.Range(0,7).Select(Function(i) DisplayStudentData(i)).ToArray())
Why isn't DisplayStudentData just an array in the first place (which would make string.Format() even easier)?
on another note, If you know the string will always stay the same
you could do something like
dim myTestString = "{0} {1} {2} {3} {4} {5} {6}"
dim result as string = string.format(myTeststring, "testFirstName",
"testTitle", "testClass", "testTeacher", "testDept", "testEmail", "testPhone")
you could always make it a property of your studentdata object.

Resources