浏览一个字符串并检查是否包含一个字符并将字符保存在列表中

时间:2015-06-17 07:29:32

标签: c# string string-split

我想通过一个字符串,并检查字符串中是否有可用的字符。 例如: 我的字符串是:string test = "100+20+3-17+2" 所以现在我通过我的字符串检查字符:

List<string> numbers= new List<string>();
foreach( char c in test)
{
   if (c =='+'|| c =='-'||c =='/'||c =='*')
   {
     //Now here i want to save all chars before '+' '-'  '/'  '*' in my list numbers. in this example: save 100, 20,3,17,2 in my list
   }
}

你会怎么做?

4 个答案:

答案 0 :(得分:2)

只需用字符

分割字符串
List<string> numbers = new List<string>();
string test = "100+20+3-17+2";
char[] chars = new char[] { '+', '-', '*', '/' };
numbers = test.Split(chars).ToList();

答案 1 :(得分:1)

我选择StringBuilder。当您查看原始字符串以检查字符时,将它们添加到StringBuilder中。当你找到一个splitter char时,在列表中添加StringBuilder.ToString并清空StringBuilder。

代码应该类似于以下内容(尚未测试过):

CREATE SCHEMA IF NOT EXISTS `fyp` ;
USE `fyp` ;



-- -----------------------------------------------------
-- Table `fyp`.`country`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `fyp`.`country` ;

CREATE TABLE IF NOT EXISTS `fyp`.`country` (
`country_id` INT NOT NULL AUTO_INCREMENT,
`country` VARCHAR(45) NOT NULL,
PRIMARY KEY (`country_id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `fyp`.`user`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `fyp`.`user` ;

CREATE TABLE IF NOT EXISTS `fyp`.`user` (
`id` INT NOT NULL AUTO_INCREMENT,
`user_name` VARCHAR(45) NOT NULL,
`password` VARCHAR(45) NOT NULL,
`email_address` VARCHAR(45) NOT NULL,
`date_of_birth` DATE NOT NULL,
`country_id` INT NOT NULL,
`gender_id` INT NOT NULL,
`role_id` INT NOT NULL,
`last_login` TIMESTAMP NULL,
PRIMARY KEY (`id`),
INDEX `fk_user_country1_idx` (`country_id` ASC),
INDEX `fk_user_gender1_idx` (`gender_id` ASC),
INDEX `fk_user_role1_idx` (`role_id` ASC),
CONSTRAINT `user_country_key`
FOREIGN KEY (`country_id`)
REFERENCES `fyp`.`country` (`country_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `user_gender_key`
FOREIGN KEY (`gender_id`)
REFERENCES `fyp`.`gender` (`gender_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `user_role_key`
FOREIGN KEY (`role_id`)
REFERENCES `fyp`.`role` (`role_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;



-- -----------------------------------------------------
-- Table `fyp`.`book`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `fyp`.`book` ;

CREATE TABLE IF NOT EXISTS `fyp`.`book` (
`book_id` INT NOT NULL AUTO_INCREMENT,
`title` VARCHAR(45) NOT NULL,
`ISBN` VARCHAR(45) NOT NULL,
`book_desc` VARCHAR(100) NOT NULL,
`year_published` VARCHAR(45) NOT NULL,
`year_of_birth` YEAR NOT NULL,
`image` VARCHAR(45) NULL,
`genre_id` INT NOT NULL,
`publisher_id` INT NOT NULL,
`user_id` INT NOT NULL,
 PRIMARY KEY (`book_id`),
 INDEX `fk_book_publishers1_idx` (`publisher_id` ASC),
 INDEX `fk_book_user1_idx` (`user_id` ASC),
 INDEX `fk_book_genre1_idx` (`genre_id` ASC),
 CONSTRAINT `book_publishers_key`
 FOREIGN KEY (`publisher_id`)
 REFERENCES `fyp`.`publishers` (`publisher_id`)
 ON DELETE NO ACTION
 ON UPDATE NO ACTION,
 CONSTRAINT `book_user_key`
 FOREIGN KEY (`user_id`)
 REFERENCES `fyp`.`user` (`id`)
 ON DELETE NO ACTION
 ON UPDATE NO ACTION,
 CONSTRAINT `book_genre_key`
 FOREIGN KEY (`genre_id`)
 REFERENCES `fyp`.`genre` (`genre_id`)
 ON DELETE NO ACTION
 ON UPDATE NO ACTION)
 ENGINE = InnoDB;

答案 2 :(得分:1)

如果char不在'+',' - ','/','*',你可以在字符串中连接字符。当操作员到来时,您可以将sting添加到列表,并清空字符串

List<string> numbers= new List<string>();
string curNumber="";
foreach( char c in test)
{
   if (c =='+'|| c =='-'||c =='/'||c =='*')
   {
     numbers.Add(curNumber);
     curNumber="";
   }
   else
   {
     //also you can add operators in other list here
     curNumber+=c.ToString();
   }
}
numbers.Add(curNumber);

答案 3 :(得分:0)

使用String.Split(Char[])获取分割字符串的数组