GVKun编程网logo

PHP STRING 陷阱原理说明(php string函数)

1

此处将为大家介绍关于PHPSTRING陷阱原理说明的详细内容,并且为您解答有关phpstring函数的相关问题,此外,我们还将为您介绍关于'string'类型的参数不能分配给'`${string}`类

此处将为大家介绍关于PHP STRING 陷阱原理说明的详细内容,并且为您解答有关php string函数的相关问题,此外,我们还将为您介绍关于'string' 类型的参数不能分配给 '`${string}` 类型的参数 | `${string}.${string}` | `${string}.${number}`'、adam lambert trespassing PHP STRING 陷阱原理说明、C# @string $string $@string、c# String ,String[] 和 List之间的转换的有用信息。

本文目录一览:

PHP STRING 陷阱原理说明(php string函数)

PHP STRING 陷阱原理说明(php string函数)

A string is series of characters.
String access and modification by character
Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose.
Note: Strings may also be accessed using braces, as in $str{42}, for the same purpose. However, this syntax is deprecated as of PHP 5.3.0. Use square brackets instead, such as $str[42].
Warning
Writing to an out of range offset pads the string with spaces. Non-integer types are converted to integer. Illegal offset type emits E_NOTICE. Negative offset emits E_NOTICE in write but reads empty string. Only the first character of an assigned string is used. Assigning empty string assigns NUL byte.

以上都是 php manual 中的原话。
需要注意的时候,我们访问数组的时候 都是使用方括号“[]”,string作为一个也可以使用操作符“[]”进行访问。但是,需要注意的一点就是,访问字符串时候,操作符“[]”中的内容会被转化为int类型的。
eg: $str =''123456'';
echo $str[''php''];//结果是1,因为offset ‘php''转化为integer为0,既是访问的是字符串的第一个字符.
var_dump(isset($str[''php'']));//结果是bool(true) 原理同上。
所以,在我们使用isset判断一个设置是否存在某个键时候,应该先判断试下,传递过来的变量是否是数组,然后再判断是否是存在指定的key
eg://如果需要判断传递过来的数组是否存在''php''这个key时候,比较安全的做法为:

复制代码 代码如下:


function is_set($arr, $key){
if (is_array($arr) && isset($arr[$key])) {
//存在该值的逻辑
} else{
//$arr不是数组 或者 数组$arr不存在key $key的逻辑
}
}


如果 上面的函数 没有添加 is_array 的判断,当传递一个 字符串过来的时候, 结果就不是我们预想的那样了。

仅此为记,以免以后也出现类似的问题。

'string' 类型的参数不能分配给 '`${string}` 类型的参数 | `${string}.${string}` | `${string}.${number}`'

'string' 类型的参数不能分配给 '`${string}` 类型的参数 | `${string}.${string}` | `${string}.${number}`'

如何解决''string'' 类型的参数不能分配给 ''`${string}` 类型的参数 | `${string}.${string}` | `${string}.${number}`''

我已将 react-hook-forms 从 v.6 迁移到 v.7。

更改register方法后,如migration guide中指出的,出现如下错误:

''string'' 类型的参数不能分配给 ''${string} 类型的参数 | ${string}.${string} | ${string}.${number}''。 TS2345

注册需要一个字符串名称,我正确地提供了一个参数,它肯定是一个字符串,但无论如何它不接受我的参数,如果我不完全传递字符串。

非常感谢有类似问题或任何想法的任何人。提前致谢!

解决方法

react-hook-form v7 中的

register() 不接受字符串,但字符串 literal。这是 register 的定义:

export declare type UseFormRegister<TFieldValues extends FieldValues> = <
  TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
>(
  name: TFieldName,options?: RegisterOptions<TFieldValues,TFieldName>
) => RefCallbackHandler;

TFieldName 是文字的联合。文字是特定的字段名称值。请参见下面的示例:

interface IFormValues {
  firstName: string;
  lastName: string;
}
const { register,handleSubmit } = useForm<IFormValues>();
const firstNameId = ''firstName''; // string literal
const lastNameId: string = ''lastName''; // string

return (
  <form onSubmit={handleSubmit(onSubmit)}>
    // this works because first argument is ''firstName'' | ''lastName''
    <input {...register(firstNameId)} />
    // this does not work because it''s just a string instead of one of the
    // two literal values
    <input {...register(lastNameId)} />
    <input type="submit" />
  </form>
);

解决方案

offical example 中提出的解决方案对我来说有点冗长,但目前有效。

您需要将寄存器名称转换为文字:

<input key={field.id} {...register(`test.${index}.test` as const)}  />

但是,如果 index 是数字,则上述解决方案不起作用。当您将文字与数字混合时,如下所示:

`test.${index}.test`

因为 index 可以是任意数字,所以 typescript 无法将其强制转换为精确值。文档中提出的解决方法是手动转换为文字:

{...register(`test.${index}.test` as `test.0.test`)}

adam lambert trespassing PHP STRING 陷阱原理说明

adam lambert trespassing PHP STRING 陷阱原理说明

A string is series of characters.
String access and modification by character
Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose.
Note: Strings may also be accessed using braces, as in $str{42}, for the same purpose. However, this syntax is deprecated as of PHP 5.3.0. Use square brackets instead, such as $str[42].
Warning
Writing to an out of range offset pads the string with spaces. Non-integer types are converted to integer. Illegal offset type emits E_NOTICE. Negative offset emits E_NOTICE in write but reads empty string. Only the first character of an assigned string is used. Assigning empty string assigns NUL byte.
以上都是 php manual 中的原话。
需要注意的时候,我们访问数组的时候 都是使用方括号“[]”,string作为一个也可以使用操作符“[]”进行访问。但是,需要注意的一点就是,访问字符串时候,操作符“[]”中的内容会被转化为int类型的。
eg: $str =''123456'';
echo $str[''php''];//结果是1,因为offset ‘php''转化为integer为0,既是访问的是字符串的第一个字符.
var_dump(isset($str[''php'']));//结果是bool(true) 原理同上。
所以,在我们使用isset判断一个设置是否存在某个键时候,应该先判断试下,传递过来的变量是否是数组,然后再判断是否是存在指定的key
eg://如果需要判断传递过来的数组是否存在''php''这个key时候,比较安全的做法为:

复制代码 代码如下:


function is_set($arr, $key){
if (is_array($arr) && isset($arr[$key])) {
//存在该值的逻辑
} else{
//$arr不是数组 或者 数组$arr不存在key $key的逻辑
}
}


如果 上面的函数 没有添加 is_array 的判断,当传递一个 字符串过来的时候, 结果就不是我们预想的那样了。
仅此为记,以免以后也出现类似的问题。

以上就介绍了adam lambert trespassing PHP STRING 陷阱原理说明,包括了adam lambert trespassing方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

C# @string $string $@string

C# @string $string $@string

@string  保证换行后也属于同一个字符串  (请特别注意 \r\n 这样也会直接输入,不在起到换行的效果)

string execSql = @" SELECT

  T1.ProcInstID,T1.Folio,T1.ApplyID,T2.RowID,T2.CurWorkFlowStep,T2.CurAuditor,T2.ModDate
   FROM dbo.WF_ApplyInstance T1";

$string  让 {} 里变为可编译,能输入参数,  要输出 {}  就需要 {{  }}

string name = "gyg";

string sql = $"select * from member where name = ''{name}''";

$@string 可同步实现上面的两个效果

比如:

string type = "测试";
var sql = $@"
  SELECT
    T1.ProcInstID,
    T1.Folio,
    T2.RowID,
    T2.CurWorkFlowStep,
    T2.CurAuditor,
    T2.ModDate
  FROM
    dbo.WF_ApplyInstance T1
  RIGHT JOIN dbo.WF_ApplyInstanceStateDetail T2 ON T2.ApplyID = T1.ApplyID
  WHERE
    T1.ApplyTypeName = ''{type}''
  AND T1.CurState = '' 审批中 ''";

c# String ,String[] 和 List<String>之间的转换

c# String ,String[] 和 List之间的转换

 C#对字符串进行处理时,经常需要进行String,String[]和List<String>之间的转换

本文分析一下它们的差异和转换

一.

  1. String > String[]

String s = "ab cd ef gh";
String[] sArray = s.Split('' '');

  2. String[] > String

string[] sArray = {"ab", "cd", "ef", "gh"};
string s = String.Join(" ", sArray);
//s = "ab cd ef gh";

  3.String[] > List<String>

string[] sArray = { "ab", "cd", "ef", "gh" };
List<String> list = new List<string>(sArray);

  4.List<String> > String[]

List<String> list = new List<string>();
list.Add("ab");            
list.Add("cd");            
list.Add("ef");            
list.Add("gh");        
string[] sArray = list.ToArray();

  5.String和List<String>之间的转换可以使用String[]来中转完成

二.

  1. String类型有很多常用的字符串操作成员

    

    字符串是不可变的,虽然这些方法看起来都会改变字符串对象,其实,它们不会改变而是返回了新的

    副本。对于一个String,任何“改变”都会分配一个新的恒定字符串。 

String s = "ab cd ef gh";
Console.WriteLine("{0}", s.ToUpper());
Console.WriteLine("{0}", s);
/*
返回结果:
AB CD EF GH
ab cd ef gh
*/

    2. String[]是定长的,String[]一般是在确定字符串数组的长度的情况下使用 

    3. List< String >一般在数组的长度会发生变化的情况下使用,例如在数组中间插入一个字符串

 

关于PHP STRING 陷阱原理说明php string函数的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于'string' 类型的参数不能分配给 '`${string}` 类型的参数 | `${string}.${string}` | `${string}.${number}`'、adam lambert trespassing PHP STRING 陷阱原理说明、C# @string $string $@string、c# String ,String[] 和 List之间的转换等相关知识的信息别忘了在本站进行查找喔。

本文标签: