How to check if the string contains only / symbol in it ? The string may have more than one / symbols with white spaces in between. In such cases i want to consider that the string is empty.
In c# the answer would be:
if (yourText.Count(x => (x != '/') && (x != ' ')) > 0)
{
//not empty
}
Basicly: "if the count of all the characters which are not "/" or a space is greater than 0"
I have not done VB in a while, but translating it should not be hard. You could just do for-loop approach
Dim isEmpty as Boolean = true;
For (Dim i as Integer = 0 to yourText.Length - 1)
If (yourText[i] <> "/"C And yourText[i] <> " "C) Then
isEmpty = false;
End If
Next
Again, I am doing this completely from memory, so excuse me if my syntax is off. Correct my code if I have an error.
Related
A palindrome is a word or phrase that reads the same backwards as forwards. Some common
examples are "Mom", "Dad", and “Bob" or even the sentence "Madam, I'm Adam." When
determining a palindrome, the case of letters is ignored, as is any spacing or punctuation if the
phrase happens to be a sentence. You need to create a web page having two text boxes and a
button. User will enter any string in text box and size of palindrome that he wants to search in the
string. After button press, you need to find all sub-strings of the given size, which are palindrome by
using AJAX and inform the user about the result. If no palindrome sub-string of the given size is
found, inform the user with a message.
You need to use button outside update panel with a trigger and update progress control as well.
Thread.Sleep(2000);
string inputstring = TextBox1.Text;
string s = TextBox1.Text.ToString();
string str = string.Empty;
for (int j = s.Length - 1; j >= 0; j--)
{
str = str + s[j];
}
//for(String i=TextBox2.Text;i=str.Length)
if (s == str)
{
Label1.Text = s + " is palindrom";
}
else
{
Label1.Text = s + " is not palindrom";
}
Code Image
It looks like there is a builtin function, VerCompare, but it requires strings that have all four components, e.g. 1.2.3.4. I haven't tried to do string manipulation in InstallScript and was hoping someone already had the code to take a version string and add .0's as necessary.
Needs some error checking, but here's the general idea:
prototype NUMBER CompareVersions(STRING, STRING);
prototype STRING FormatVersion(STRING);
function NUMBER CompareVersions(leftVersion, rightVersion)
STRING formattedLeftVersion, formattedRightVersion;
begin
formattedLeftVersion = FormatVersion(leftVersion);
formattedRightVersion = FormatVersion(rightVersion);
return VerCompare(formattedLeftVersion, formattedRightVersion, VERSION);
end;
function STRING FormatVersion(version)
STRING formattedVersion;
LIST tokens;
NUMBER count;
begin
tokens = ListCreate(STRINGLIST);
StrGetTokens(tokens, version, ".");
count = ListCount(tokens);
ListSetIndex(tokens, LISTLAST);
while (count < 4)
ListAddString(tokens, "0", AFTER);
count = count + 1;
endwhile;
StrPutTokens(tokens, formattedVersion, ".", FALSE);
ListDestroy(tokens);
return formattedVersion;
end;
I have a hidden field that gets populated with a javascript array of ID's. When I try to iterate the hidden field(called "hidExhibitsIDs") it gives me an error(in the title).
this is my loop:
foreach(string exhibit in hidExhibitsIDs.Value)
{
comLinkExhibitToTask.Parameters.AddWithValue("#ExhibitID", exhibit);
}
when I hover over the .value it says it is "string". But when I change the "string exhibit" to "int exhibit" it works, but gives me an internal error(not important right now).
You need to convert string to string array to using in for loop to get strings not characters as your loop suggests. Assuming comma is delimiter character in the hidden field, hidden field value will be converted to string array by split.
foreach(string exhibit in hidExhibitsIDs.Value.Split(','))
{
comLinkExhibitToTask.Parameters.AddWithValue("#ExhibitID", exhibit);
}
Value is returning a String. When you do a foreach on a String, it iterates over the individual characters in it. What does the value actually look like? You'll have to parse it correctly before you try to use the data.
Example of what your code is somewhat doing right now:
var myString = "Hey";
foreach (var c in myString)
{
Console.WriteLine(c);
}
Will output:
H
e
y
You can use Char.ToString in order to convert
Link : http://msdn.microsoft.com/en-us/library/3d315df2.aspx
Or you can use this if you want convert your tab of char
char[] tab = new char[] { 'a', 'b', 'c', 'd' };
string str = new string(tab);
Value is a string, which implements IEnumerable<char>, so when you foreach over a string, it loops over each character.
I would run the debugger and see what the actual value of the hidden field is. It can't be an array, since when the POST happens, it is converted into a string.
On the server side, The Value property of a HiddenField (or HtmlInputHidden) is just a string, whose enumerator returns char structs. You'll need to split it to iterate over your IDs.
If you set the value of the hidden field on the client side with a JavaScript array, it will be a comma-separated string on the server side, so something like this will work:
foreach(string exhibit in hidExhibitsIDs.Value.Split(','))
{
comLinkExhibitToTask.Parameters.AddWithValue("#ExhibitID", exhibit);
}
public static string reversewordsInsentence(string sentence)
{
string output = string.Empty;
string word = string.Empty;
foreach(char c in sentence)
{
if (c == ' ')
{
output = word + ' ' + output;
word = string.Empty;
}
else
{
word = word + c;
}
}
output = word + ' ' + output;
return output;
}
bool Res = false;
DataView DV = new DataView(DT);
DV.RowFilter = "Trim(Originator)='"+OrginatorName.Trim()+"'";
if (DV.Count > 0)
{
Res = true;
}
I need to get "Originator" from the database and compare it with the OrginatorName to check duplicate values. I need to remove all the white spaces before checking.
For example, the function must consider "John Van" to be the same as "JohnVan". My above code doesn't work. How can I achieve this?
String.Trim() removes whitespace from the beginning and end only, not in the middle. You want to use the String.Replace() method
DV.RowFilter = "Trim(Originator)='"+OrginatorName.Replace(" ", "")+"'";
this line should be
DV.RowFilter = "Trim(Originator)='"+OrginatorName.Replace(" ","")+"'";
User .Replace instead of .Trim()
The following code:
If checkboxList.Items(i).Selected Then
.Fields("DESC1").Value += checkboxList.Items(i).Text + ", "
End If
should produce output such as "A, B, C,(space)", which will then be bound to a dynamically created GridView. I would like to remove the last two-char string, that is ",(space)". How can I do this?
Take a look at String.Join, which may do what you want, without the need to manipulate the final two characters.
I wouldn't add them on in the first place :) try
If checkboxList.Items(i).Selected Then
if .Fields("DESC1").Value Is System.DbNull.Value then
.Fields("DESC1").Value = checkboxList.Items(i).Text
else
.Fields("DESC1").Value += ", " + checkboxList.Items(i).Text
End If
End If
For info, string concatenation is expensive. It looks (from the i and from the results) like you should really be using a StringBuilder; some rough pseudo-code (in C#, but trivial to translate to VB.Net):
StringBuilder sb = new StringBuilder();
for(int i = 0 ; i < checkboxList.Items.Count ; i++) {
if(checkboxList.Items[i].Selected) {
if(sb.Length > 0) sb.Append(", "); // add separator
sb.Append(checkboxList.Items[i].Text); // add text
}
}
someOjb.Fields("DESC1") = sb.ToString(); // use result
You can use .TrimEnd(", ".ToCharArray()) on the string, or you can use SubString:
strLetters.Substring(0, strLetters.Length - 2)
It seems you just want to get "A, B, C" from "A, B, C, ". A bit of simple string manipulation should do the job:
Dim input = "A, B, C, "
Dim result = input.Substring(0, input.LastIndexOf(","))
This is more versatile than simply removing the last two characters, since it looks for the last comma, which is what I believe you are after.
Of course, the fact that you're adding on these two chars in the first place sounds a bit dodgy. I'd need to see more context to show how this can be avoided, however.
use
.Fields("DESC1").Value += checkboxList.Items(i).Text + ", "
. Fields("DESC1").Value = .Fields("DESC1").Value.TrimRight(new []{',',' '});
PS:- sorry if it is not valid vb syntax :)
There is also just "Remove":
string k = "okay";
string s = k.Remove(k.Length - 2, 2);
This will remove all trailing , and/or [space]:
.Fields("DESC1").Value = .Fields("DESC1").Value.TrimRight(", ".ToCharArrray())
var selectedValues = checkboxList.Items
.Where(i => i.Selected)
.Select(i => i.Fields("DESC1").Value);
var result = String.Join(", ", selectedValues);
Does VB have a ternary if operator?
If checkboxList.Items(i).Selected Then
.Fields("DESC1").Value += checkboxList.Items(i).Text + (i == checkboxList.Items.Length-1 ? "" : ", ")
End If