Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i want to create arrays with different *names (the text out of a textField should be the name of the array) when an IBAction function is called. How do i get the text out of the text field and make it the *name of the array?
- (IBAction)createButton:(id)sender {
NSMutableString *string = [[NSMutableString alloc] init];
[string setString:[textField text]];
NSMutableArray *string = [[NSMutableArray alloc] init];
}
Create a NSDictionary and use the string as key, and array as value.
try this . . .
- (IBAction)createButton:(id)sender {
NSString *string = [textField text];
NSMutableArray *array = [string componentsSeparatedByString:#" "];
}
if names are separated by space , then provide space , if they are separated by comma ',' then
// NSMutableArray *array = [string componentsSeparatedByString:#","];
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 days ago.
This post was edited and submitted for review yesterday.
Improve this question
I want to replace a String data with a new one where I do not know what is inside the string as it may be empty, spaces, alphabets, numerics, special characters, mix, etc. It is simple as below
String randomString = "Some Old String"; // It will be any string or may be an empty string too
print("Old String ${randomString}"); // Output = Some Old String
randomString = "Some New String";
print("New String ${randomString}"); // Output = Some New String
But due to some limitations (no need to define here), I can't use the upper process. I would like to do this with only sub-functions like .replaceAll, .replaceRange, .replaceMap, .replaceFirst, .replaceFirstMap etc. For that, I tried following.
String randomString = ""; // It will be any string or maybe an empty string too
print("Old String ${randomString}"); // Output = ""
randomString.replaceAll(RegExp(r"/\W/g"),"Some New String");
print("New String ${randomString}"); // Output = "" (Showing Empty)
What should be the regex to select all and replace with alphabets, digits, special characters, spaces, empty, etc.?
I am currently using RegExp(r"/\W/g") so tell me the correct one for my need.
Live DEMO:
jdoodle.com/ia/DVm
You can achieve this by creating TextInputFormatter class and pass it to inputFormatters parameter in TextField, here's an example:
class ReplaceTextInputFormatter extends TextInputFormatter {
#override
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
// TODO replace the [[oldValue] with [newValue] for your needs
return newValue;
}
}
TextField(
inputFormatters: [ReplaceTextInputFormatter()],
)
Edit after #Muhammad Hassan's comment about no need for widgets.
We can achieve this using replaceRange from 0 to the string length, for example:
randomString.replaceRange(0, s.length, 'replacement');
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I've asked to write code that gets a char array(sentence), if the there is an 'i' in the sentence I need to add the letter 'b' the letter 'i' again like this example:
pig -> pibig
I tried to use string.h functions but I didn't succeed to make it right.
Use std::string in string header file, and std::string::insert whenever you need to insert a char in string:
std::string my_string = "my satringa";
for (size_t i = 0; i < my_string.length(); ++i)
{
if (my_string.at(i) == 'a')
{
my_string.insert(i + 1, "b");
}
}
std::clog << my_string << std::endl;
Output:
> my sabtringab
If you are forced to use C-style strings, don't worry do all of your operations on std::string and then take the underlying stored string with std::string::c_str() as a C-style string (and don't forget to take a copy).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm sick and tired of solving why my ch[0] is of value "Thomas EdisonÇ#", when it should be "Thomas Edison"
int main(){
using namespace std;
ifstream in("U2.txt");
int n;
in>>n; //n=rows, so in every line there will be "name surname", time, money
char ch[n][21]; //I'm trying to get Name+Surname which must be 20 char long
in.read(ch[0], 20);
cout << ch[0]; //but getting Thomas EdisonÇ#
return 0;}
It works on one dimentional ch[21], but there's gonna be lots of values so I want to use ch[n][21]
Any other out of my box solution is welcome, I'm tired
You are forgetting that C strings need to be nul terminated
in.read(ch[0], 20);
ch[0][20] = '\0'; // add the nul terminator
cout << ch[0]; // now correct output
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
With this code below I dont know how to delete the undesired characters appearing at the end of the message array. It is compulsory for me to use char, can't use strings, because of the rest of my code.
recvbuf is also a char* recvbuf=new char
char* message=new char[140];
for (int i=1; i<141; i++){
message[i-1]=recvbuf[i];
}
printf("Message: %s\n", message);
delete[]recvbuf;
Though it is recommended you use strings to implement this code, the problem can be fixed by manually appending a null character \0 at the end of your char array.
You can introduce it as:
char* message=new char[141];
for (int i=1; i<141; i++){
message[i-1]=recvbuf[i];
}
message[140] = '\0'; //newly introduced line.
printf("Message: %s\n", message);
delete[]recvbuf;
NOTE 1: The size of the array was increased from 140 to 141 during initialization to make room for the \0 character at the end.
Cheers!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have a string which represents a file name, and I want to remove the extension, so erasing everything after the ".". What would be the best way ? Thanks.
Below code can be used for the same..
int npos = str.find_last_of('.');
str = str.substring(0,npos);
If you're on Windows, the following function will do the trick:
std::wstring StripFileExtension(std::wstring fileName)
{
WCHAR tempBuffer[MAX_PATH];
if (fileName.empty())
{
return TEXT("");
}
wcscpy(tempBuffer, fileName.c_str());
PathRemoveExtension(tempBuffer);
return tempBuffer;
}
you can use std::string , and copy each character to new string
std::string name = "filename.jpg", newname ="";
int thelength = 0;
for(int i=name.length();i>0;i--){
if( name[i] != '.'){
thelength++;
}
else{
break;
}
}
for(int i=0;i<(name.length()-thelength);i++){
newname+=name[i];
}