Need to cut a string(Table Name) from Query in c# - string

My String/Query looks like this
insert into Employee Values(1,2,'xxx');
update Employee2 set col1='xxx' where col2='yyy';
select * from Employee3;
I need to take/have TableName alone. Table name won't be constant it will be differed(Employee,Employee2,Employee3) according to DB. I'm new to C# please help me. Thanks in advance.

To get the name of a table from a query (or in this case, a string named 'sql'), try the following:
string sql = "select * from table ";
int index1 = 0;
int index2 = 0;
int currentIndex = 0;
int numSpaces = 0;
char[] chArray = sql.ToCharArray();
foreach (char c in chArray)
{
if (c == ' ')
{
numSpaces++;
if (numSpaces == 3)
index1 = currentIndex;
if (numSpaces == 4)
{
index2 = currentIndex;
break;
}
}
currentIndex++;
}
int length = index2 - index1;
string tableName = sql.Substring(index1, length);
MessageBox.Show(tableName);
Warning - this solution is based on finding the word between the 3rd and 4th space character. This limits you to have a very predictable query structure - more complicated queries may not work with this solution. Your query structure needs to be:
"select column_name1,column_name2,column_name3 from table "
Your query must not have spaces between columns and must have a space at the end aswell. Sorry for the limitations but its the best I can come up with ;)

Related

How get string from skip in DOORS DXL

I am using following code to get string from skip function. But i am getting integer numbers. I will appreciate if someone can help me out.
int csvToSkip(string csv, Skip skip, char delimeter)
{
int i = 0
int j = 0
int index = 0
for (i = 0; i < length(csv); i++)
{
if (csv[i] == delimeter)
{
put(skip, 0, "1")
j = i + 1
}
else if (i == length(csv) - 1)
{
put(skip, 1, "2")
}
}
return(index)
}
Skip mySkip=create;
string test="hi this is test;for another test";
char delimiter =';';
int x=csvToSkip(test, mySkip, delimiter );
print x;
for sValue in mySkip do
{
print (int key mySkip) " " sValue "\n";
}
This gives me following result
0
0 204534013
1 204534015
You did not declare sValue, so DXL guessed wrongly what data type the values have.
The first chapter of DXL Manual -> Language fundamentals, called "Auto-Declare", explains how you can disable the auto-declare functionality. If you do this, DOORS will warn you when you access undeclared variables.

Add comma sequentially to string in C#

I have a string.
string str = "TTFTTFFTTTTF";
How can I break this string and add character ","?
result should be- TTF,TTF,FTT,TTF
You could use String.Join after you've grouped by 3-chars:
var groups = str.Select((c, ix) => new { Char = c, Index = ix })
.GroupBy(x => x.Index / 3)
.Select(g => String.Concat(g.Select(x => x.Char)));
string result = string.Join(",", groups);
Since you're new to programming. That's a LINQ query so you need to add using System.Linq to the top of your code file.
The Select extension method creates an anonymous type containing the char and the index of each char.
GroupBy groups them by the result of index / 3 which is an integer division that truncates decimal places. That's why you create groups of three.
String.Concat creates a string from the 3 characters.
String.Join concatenates them and inserts a comma delimiter between each.
Here is a really simple solution using StringBuilder
var stringBuilder = new StringBuilder();
for (int i = 0; i < str.Length; i += 3)
{
stringBuilder.AppendFormat("{0},", str.Substring(i, 3));
}
stringBuilder.Length -= 1;
str = stringBuilder.ToString();
I'm not sure if the following is better.
stringBuilder.Append(str.Substring(i, 3)).Append(',');
I would suggest to avoid LINQ in this case as it will perform a lot more operations and this is a fairly simple task.
You can use insert
Insert places one string into another. This forms a new string in your C# program. We use the string Insert method to place one string in the middle of another one—or at any other position.
Tip 1:
We can insert one string at any index into another. IndexOf can return a suitable index.
Tip 2:
Insert can be used to concatenate strings. But this is less efficient—concat, as with + is faster.
for(int i=3;i<=str.Length - 1;i+=4)
{
str=str.Insert(i,",");
}

Is the number of Parameters in the IN-Operator in Cassandra limited?

I have a pretty simple question which I can't find an answer to on the Internet or on stackoverflow:
Is the number of Parameters in the IN-Operator in Cassandra limited?
I have made some tests with a simple table with Integer-Keys from 1 to 100000. If I put the keys from 0 to 1000 in my IN-Operator (like SELECT * FROM test.numbers WHERE id IN (0,..,1000)) I get the correct number of rows back. But for example for 0 to 100000 I always get only 34464 rows back. And for 0 to 75000 its 9464.
I am using the Datastax Java Driver 2.0 and the relevant codeparts look like the following:
String query = "SELECT * FROM test.numbers WHERE id IN ?;";
PreparedStatement ps = iot.getSession().prepare(query);
bs = new BoundStatement(ps);
List<Integer> ints = new ArrayList<Integer>();
for (int i = 0; i < 100000; i++) {
ints.add(i);
}
bs.bind(ints);
ResultSet rs = iot.getSession().execute(bs);
int rowCount = 0;
for (Row row : rs) {
rowCount++;
}
System.out.println("counted rows: " + rowCount);
It's also possible that I'm binding the list of Integers in a wrong way. If that's the case I would appreciate any hints too.
I am using Cassandra 2.0.7 with CQL 3.1.1.
This is not a real-limitation but a PreparedStatement one.
Using a BuiltStatement and QueryBuilder I didn't have any of these problems.
Try it yourself:
List<Integer> l = new ArrayList<>();
for (int i = 0; i < 100000; i++) {
l.add(i);
}
BuiltStatement bs = QueryBuilder.select().column("id").from("test.numbers").where(in("id", l.toArray()));
ResultSet rs = Cassandra.DB.getSession().execute(bs);
System.out.println("counted rows: " + rs.all().size());
HTH,
Carlo

Extract Substring from String c#

I have set of code which returns me a value "2;#bbbb" where as i want to achieve bbbb.
Below is the code written by me.
SPListItemCollection col = StationaryList.Items;
for (int i = 0; i < col.Count; i++)
{
SPListItem item = col[i];
categoryName=item["QuizCategoryName"].ToString();
}
Please tell me what do i do to achieve this.
Actual Output: "2;#bbbb"
Expected Output: bbbb
string str = categoryName;
string[] result = str.Split('#');
if(result.Length > 1)
Response.Write(result[1]); // your expected output
This is format of lookup field which can be extracted using next sintaxys:
SPFieldLookupValue f = new SPFieldLookupValue("your text here");
string value = f.LookupValue;
This way can be extracted ID which in your case is 2
Andrew

Search an integer in a row-sorted two dim array, is there any better approach?

I have recently come across with this problem,
you have to find an integer from a sorted two dimensional array. But the two dim array is sorted in rows not in columns. I have solved the problem but still thinking that there may be some better approach. So I have come here to discuss with all of you. Your suggestions and improvement will help me to grow in coding. here is the code
int searchInteger = Int32.Parse(Console.ReadLine());
int cnt = 0;
for (int i = 0; i < x; i++)
{
if (intarry[i, 0] <= searchInteger && intarry[i,y-1] >= searchInteger)
{
if (intarry[i, 0] == searchInteger || intarry[i, y - 1] == searchInteger)
Console.WriteLine("string present {0} times" , ++cnt);
else
{
int[] array = new int[y];
int y1 = 0;
for (int k = 0; k < y; k++)
array[k] = intarry[i, y1++];
bool result;
if (result = binarySearch(array, searchInteger) == true)
{
Console.WriteLine("string present inside {0} times", ++ cnt);
Console.ReadLine();
}
}
}
}
Where searchInteger is the integer we have to find in the array. and binary search is the methiod which is returning boolean if the value is present in the single dimension array (in that single row).
please help, is it optimum or there are better solution than this.
Thanks
Provided you have declared the array intarry, x and y as follows:
int[,] intarry =
{
{0,7,2},
{3,4,5},
{6,7,8}
};
var y = intarry.GetUpperBound(0)+1;
var x = intarry.GetUpperBound(1)+1;
// intarry.Dump();
You can keep it as simple as:
int searchInteger = Int32.Parse(Console.ReadLine());
var cnt=0;
for(var r=0; r<y; r++)
{
for(var c=0; c<x; c++)
{
if (intarry[r, c].Equals(searchInteger))
{
cnt++;
Console.WriteLine(
"string present at position [{0},{1}]" , r, c);
} // if
} // for
} // for
Console.WriteLine("string present {0} times" , cnt);
This example assumes that you don't have any information whether the array is sorted or not (which means: if you don't know if it is sorted you have to go through every element and can't use binary search). Based on this example you can refine the performance, if you know more how the data in the array is structured:
if the rows are sorted ascending, you can replace the inner for loop by a binary search
if the entire array is sorted ascending and the data does not repeat, e.g.
int[,] intarry = {{0,1,2}, {3,4,5}, {6,7,8}};
then you can exit the loop as soon as the item is found. The easiest way to do this to create
a function and add a return statement to the inner for loop.

Resources