Hi I'm currently using Microsoft Visual Studio 2015, c++ with forms and having trouble with split. I have tried many code exemples (all of them was on console application and I didn't know how to make them work for me). This is how at least I imagining code (simplified code exemple below). There is a string that you take from textBox1, then you split that string where there is a dot, and putting them in table.
String ^ text = textBox1->Text;
text->ToString()->Split('.');
tableGrid->Rows[0]->Cells[1]->Value = text;
Split does not modify text here. Instead it returns an array of the split results.
You need to capture and use the results so something like this:
String^ text = textBox1->Text;
cli::array<String^>^ pieces = text->Split('.');
for (int i = 0; i < pieces->Length; ++i) {
// Add pieces[i] to the table. Perhaps:
tableGrid->Rows[0]->Cells[i]->Value = pieces[i];
}
Related
I have an object which is trying to determine if a value it reads from on screen is the same as that passed to the object. This is a validation step and it doesn't appear to recognize them when they are the same. I have also tried trimming and lowering both values. I have also tried Test Regex Match.
Is there any way that I can get the object to recognize that they are the same, or is there a way for me to find out why they are not matching?
A strange thing. If direct comparison failed, even after trimming and with regex failed, there is probably something wrong with some of the characters. I would probably guess the spaces. Have you experienced this behaviour even on values without spaces?
Anyway, I would probably build a C# code stage like this, that accepts txt (string) and outputs col (collection):
col = new DataTable();
col.Columns.Add("Pos", typeof(decimal));
col.Columns.Add("Char", typeof(string));
col.Columns.Add("CharNum", typeof(decimal));
char[] arr = txt.ToCharArray();
for (int i = 0; i < arr.Length; i++)
{
DataRow row = col.NewRow();
row["Pos"] = i;
row["Char"] = arr[i];
row["CharNum"] = (int)arr[i];
col.Rows.Add(row);
}
The result would be like this:
Try to run the code stage on both of your values and see if there is a visible discrepancy.
The solution was to use a Remove Non Word Characters Action in Utility Strings.
String s1="Welcome To Java";
String s2="Wela To Java";
Write a Java program to get the output that come is replaced by a.
If you are sure that that is one char replacing multi chars, you can use this:
String s1="Welcome To Java";
String s2="Wela To Java";
for (int i = 1; i < s2.length()+1; i++){
char c = s2.charAt(i-1);
String part1= s2.substring (0,i-1);
String part2=s2.substring(i);
if (s1.contains(part1)&&(s1.contains(part2))){
int t1 = s1.lastIndexOf(part1)+part1.length();
int t2 = s1.indexOf(part2);
System.out.println("Found "+s1.substring(t1,t2)+ " is replaced by "+c);
}
}
Assuming your question is replace all instances of the word 'come' with 'a', you should read up pattern-matching and the .replace() method in Java. See the docs for this:
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace(java.lang.CharSequence,%20java.lang.CharSequence)
The above works nicely for either a specific string or more general patterns in the data you're working with.
P.S. Your question is unlikely to get a good answer because it's a pretty straightforward thing that requires just a quick search. I can tell you now that asking on Stack Overflow as a last resort is a good policy to follow.
I am slowly understanding things in swift, I am coming for a javascript background so it is somewhat familiar.
However variables are urking me.
in JS a variable can be
var varName = 1; //Number
var varName2 = "petey" //String
var conCat = varname + varName2; // 1petey
however in swift String vars and In var are troubling me. All I want to do is capture decimal number from user input from multiple "textField" sources and store that data to variable (which i already have setup) I need to concatinate a few of them with + then do basic math with the data in the variables with * and /.
How do I make the textFeld only capture?int or how do I convert text field strings to numbers, e.g. int?
A UITextField contains a String, not an Int, so you have to convert it, e.g.
let number : Int? = textField.text.toInt()
So, there actually is a method to convert from String to Int built-in in Swift. Beware, that it returns an optional, because the conversion may fail. So you have to check for nil before you use the variable.
For your other question, take a look at e.g. UITextField - Allow only numbers and punctuation input/keypad. Basically, you will have to adapt UITextField and filter it, once there are new entries. On iOS it might be easier, because you can show a numbers-only keyboard.
The data send from the textField is String. There are multiple ways to convert an Int to a String.
var a:String="\(2+3)" //"5"
And to concatenate a String to Int :
var b:String="Hello "+"\(3*4)" //"Hello 12"
And to Convert a String to an Int from a textField:
var b:Int=textField.text.toInt()
Use the function in swift inputmethod.intValue(); if any text is entered then it returns with a 0.
Is there a multiline string literal syntax in Matlab or is it necessary to concatenate multiple lines?
I found the verbatim package, but it only works in an m-file or function and not interactively within editor cells.
EDIT: I am particularly after readbility and ease of modifying the literal in the code (imagine it contains indented blocks of different levels) - it is easy to make multiline strings, but I am looking for the most convenient sytax for doing that.
So far I have
t = {...
'abc'...
'def'};
t = cellfun(#(x) [x sprintf('\n')],t,'Unif',false);
t = horzcat(t{:});
which gives size(t) = 1 8, but is obviously a bit of a mess.
EDIT 2: Basically verbatim does what I want except it doesn't work in Editor cells, but maybe my best bet is to update it so it does. I think it should be possible to get current open file and cursor position from the java interface to the Editor. The problem would be if there were multiple verbatim calls in the same cell how would you distinguish between them.
I'd go for:
multiline = sprintf([ ...
'Line 1\n'...
'Line 2\n'...
]);
Matlab is an oddball in that escape processing in strings is a function of the printf family of functions instead of the string literal syntax. And no multiline literals. Oh well.
I've ended up doing two things. First, make CR() and LF() functions that just return processed \r and \n respectively, so you can use them as pseudo-literals in your code. I prefer doing this way rather than sending entire strings through sprintf(), because there might be other backslashes in there you didn't want processed as escape sequences (e.g. if some of your strings came from function arguments or input read from elsewhere).
function out = CR()
out = char(13); % # sprintf('\r')
function out = LF()
out = char(10); % # sprintf('\n');
Second, make a join(glue, strs) function that works like Perl's join or the cellfun/horzcat code in your example, but without the final trailing separator.
function out = join(glue, strs)
strs = strs(:)';
strs(2,:) = {glue};
strs = strs(:)';
strs(end) = [];
out = cat(2, strs{:});
And then use it with cell literals like you do.
str = join(LF, {
'abc'
'defghi'
'jklm'
});
You don't need the "..." ellipses in cell literals like this; omitting them does a vertical vector construction, and it's fine if the rows have different lengths of char strings because they're each getting stuck inside a cell. That alone should save you some typing.
Bit of an old thread but I got this
multiline = join([
"Line 1"
"Line 2"
], newline)
I think if makes things pretty easy but obviously it depends on what one is looking for :)
I am creating a custom field where I want to replace some unicode caracters by pictures. Its like doing emoticons for blackberry device. Well I have a problem looping the caracters in the edit field and replacing the unicode caracters by images. When the text becomes too long, the loop takes too much time.
My code is as follows:
String aabb = "";
char[] chara = this.getText().toCharArray();
for (int i = loc; i < chara.length; i ++) {
Character cc = new Character(chara[i]);
aabb += cc.toString();
if (unicodeCaracter) {
//Get the location
//draw the image in the appropriate X and Y
}
}
Well this works fine, and the images are getting in the right place. But the problem is when the text becomes large, the looping is taking too much time, and the input of the text on the device becomes non friendly.
How to find the unicode caracters in a text without having to loop each time for them? Is their another way than this that I missed?
I need help with this issue. Thanks in advance
Well you're creating a new Character and a new String in each iteration of the loop, and converting the string to a character array to start with. You're also using string concatenation in a loop rather than using a StringBuffer. All of these will be hurting performance.
It's not obvious what you mean by "Unicode characters" here - all characters in Java are Unicode characters. I suspect you really want something like:
String text = this.getText();
StringBuffer buffer = new StringBuffer(text.length());
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
buffer.append(c);
if (c > 127) { // Or whatever
// Take some action
}
}
I'm assuming the "take some action" will be changing the buffer in some respect, otherwise the buffer is pointless of course... but fundamentally that's likely to be the sort of change you want.
The string concatenation in a loop is a particularly bad idea - see my article on it for more details.
What takes time is the string concatenation.
Strings are immutable in Java. Each time you do
aabb += cc.toString();
you create a new String object containing all the chars of the previous one, which must be garbage collected, plus the new ones. Use a StringBuilder to build your string:
StringBuilder builder = new StringBuilder(this.getText().length() + 100); // size estimation
char[] chara = this.getText().toCharArray();
for (int i = loc; i < chara.length; i++) {
builder.append(chara[i]);
if (unicodeCaracter) {
//Get the location
//draw the image in the appropriate X and Y
}
}
String aabb = builder.toString();
Well, besides speeding up your loop, you could also try and minimize the work load.
If the user is appending text you could store the last position you scanned previously time and start from there..
On inserts/deletes you'd need to get the caret position and scan the deleted/inserted part and maybe surrounding characters (if you have character groups instead of single characters that get replaced).
However, fixing loop performance is likely to give you a better improvement in your case, as I doubt you'll have that long strings to make that algorithmic change worthwhile.
The most important performance enhancements have already been stated but looping backwards will also help in BlackBerry apps.
Programming Tips: General Coding Tips