String.Replace()不能使用" |"

时间:2016-10-05 09:07:33

标签: c# visual-studio replace filepath

在我当前的项目中,我想动态设置connectionString。 我从默认设置中获取了占位符("|DataDirectory|")的字符串,然后我想用实际路径替换该占位符。

我的代码:

connectionString = Properties.Settings.Default.KDatabaseConnectionString;
connectionString.Replace(@"|DataDirectory|", Application.StartupPath);

我无法让它发挥作用,它什么也做不了。当我在dubug模式下运行代码时,我可以看到连接字符串包含" |DataDirectory|"但它并没有取代它。

我在这里看到很多关于string.replace()没有工作的问题,但没有关于我的问题。所以任何人都有想法?

编辑:好的,谢谢。这样一个不好的新手错误。

3 个答案:

答案 0 :(得分:7)

您没有将结果放回connectionString

尝试

connectionString = Properties.Settings.Default.KDatabaseConnectionString;
connectionString = connectionString.Replace(@"|DataDirectory|", Application.StartupPath);

答案 1 :(得分:1)

您未使用.Replace()的返回值。

换句话说,connectionString = connectionString.Replace(@"|DataDirectory|", Application.StartupPath);方法实际上并没有对变量本身做任何事情,只返回你要使用的新值。

您需要的声明是:

public string Replace(string str) { Do something... return str; }

否则你做的相当于:

Value

其中str由Reference而不是public function helpPeopleStore (Request $request) { if (Auth::check()) { $current_url = Session::get('current_url'); $user = Auth::user(); $industry = $request->input('industry'); if (empty($industry)) { Session::flash('error', 'Industry is needed'); return redirect()->back()->withInput(); } $degrees = $request->input('degrees'); if (empty($degrees)) { Session::flash('error', 'Degree is needed'); return redirect()->back()->withInput(); } $area_of_experiences = $request->input('area_of_experiences'); if (empty($area_of_experiences)) { Session::flash('error', 'area of experiences is needed'); return redirect()->back()->withInput(); } $user_industry = UserIndustry::where('user_id', $user->id)->first(); if (!empty($user_industry)) { $user_industry = $user_industry; } else { $user_industry = new UserIndustry(); } $user_degrees_exists = UserDegree::where('user_id', $user->id)->get(); if (!empty($user_degrees_exists)) { foreach ($user_degrees_exists as $user_degree_exist) { UserDegree::destroy($user_degree_exist->id); } } $user_area_of_experiences_exists = UserAreaOfExperience::where('user_id', $user->id)->get(); if (!empty($user_area_of_experiences_exists)) { foreach ($user_area_of_experiences_exists as $user_area_of_experience_exist) { UserAreaOfExperience::destroy($user_area_of_experience_exist->id); } } $user_industry->user_id = $user->id; $user_industry->industry_id = $industry; $user_industry->save(); if (!empty($degrees)) { foreach ($degrees as $degree) { $user_degrees = new UserDegree(); $user_degrees->user_id = $user->id; $user_degrees->degree_id = $degree; $user_degrees->save(); } } if (!empty($area_of_experiences)) { foreach ($area_of_experiences as $area_of_experience) { $user_area_of_experiences = new UserAreaOfExperience(); $user_area_of_experiences->user_id = $user->id; $user_area_of_experiences->area_of_experience_id = $area_of_experience; $user_area_of_experiences->save(); } } return redirect($current_url); Session::flush(); } else { return redirect('user/registration'); } Session::flush(); } 传递,然后不使用返回的值。

答案 2 :(得分:0)

<?php

// add composer autoload somewhere here...

$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();

return [
    'db' => [
        'host' => getenv('DB_HOST'),
        'user' => getenv('DB_USER'),
        'pass' => getenv('DB_PASSWORD'),
    ]
]