Skip to content

Fix remark about empty array #11625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions snippets/csharp/System/String/IndexOfAny/IndexOfAny1.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
// <Snippet1>
using System;
using System;

public class Example
public class Example1
{
public static void Main()
public static void Run()
{
char[] chars = { 'a', 'e', 'i', 'o', 'u', 'y',
// <Snippet1>
char[] chars = { 'a', 'e', 'i', 'o', 'u', 'y',
'A', 'E', 'I', 'O', 'U', 'Y' };
String s = "The long and winding road...";
Console.WriteLine("The first vowel in \n {0}\nis found at position {1}",
s, s.IndexOfAny(chars) + 1);
Console.WriteLine($"The first vowel in \n {s}\n" +
$"is found at index {s.IndexOfAny(chars)}");

// The example displays the following output:
// The first vowel in
// The long and winding road...
// is found at index 2
// </Snippet1>
}
}
// The example displays the following output:
// The first vowel in
// The long and winding road...
// is found at position 3
// </Snippet1>
18 changes: 18 additions & 0 deletions snippets/csharp/System/String/IndexOfAny/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project
{
internal class Program
{
static void Main(string[] args)
{
Example1.Run();
Example2.Run();
Example3.Run();
}
}
}
8 changes: 8 additions & 0 deletions snippets/csharp/System/String/IndexOfAny/Project.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>

</Project>
71 changes: 39 additions & 32 deletions snippets/csharp/System/String/IndexOfAny/ixany2.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,48 @@
//<snippet1>

// Sample for String.IndexOfAny(Char[], Int32)
using System;

class Sample {
public static void Main()
class Example2
{
public static void Run()
{
string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
string br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
string str = "Now is the time for all good men to come to the aid of their party.";
int start;
int at;
string target = "is";
char[] anyOf = target.ToCharArray();
//<snippet1>
string br1 = "0----+----1----+----2----+----3" +
"----+----4----+----5----+----6----+-";
string br2 = "012345678901234567890123456789" +
"0123456789012345678901234567890123456";
string str = "Now is the time for all good men " +
"to come to the aid of their party.";
int start;
int at;
string target = "is";
char[] anyOf = target.ToCharArray();

start = str.Length/2;
Console.WriteLine();
Console.WriteLine("The first character occurrence from position {0} to {1}.",
start, str.Length-1);
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);
Console.Write("A character in '{0}' occurs at position: ", target);
start = str.Length / 2;
Console.WriteLine();
Console.WriteLine("The first character occurrence " +
$"from position {start} to {str.Length - 1}:");
Console.WriteLine($"{Environment.NewLine}{br1}{Environment.NewLine}" +
$"{br2}{Environment.NewLine}{str}{Environment.NewLine}");
Console.Write($"A character in '{target}' occurs at position: ");

at = str.IndexOfAny(anyOf, start);
if (at > -1)
Console.Write(at);
else
Console.Write("(not found)");
Console.WriteLine();
}
}
/*
at = str.IndexOfAny(anyOf, start);
if (at > -1)
Console.Write(at);
else
Console.Write("(not found)");
Console.WriteLine();

/*

The first character occurrence from position 33 to 66.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.
The first character occurrence from position 33 to 66.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.

A character in 'is' occurs at position: 49
A character in 'is' occurs at position: 49

*/
//</snippet1>
*/
//</snippet1>
}
}
71 changes: 38 additions & 33 deletions snippets/csharp/System/String/IndexOfAny/ixany3.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
//<snippet1>

// Sample for String.IndexOfAny(Char[], Int32, Int32)
using System;

class Sample {
public static void Main()
class Example3
{
public static void Run()
{
string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
string br2 = "0123456789012345678901234567890123456789012345678901234567890123456";
string str = "Now is the time for all good men to come to the aid of their party.";
int start;
int at;
int count;
string target = "aid";
char[] anyOf = target.ToCharArray();
//<snippet1>
string br1 = "0----+----1----+----2----+----3----" +
"+----4----+----5----+----6----+-";
string br2 = "012345678901234567890123456789" +
"0123456789012345678901234567890123456";
string str = "Now is the time for all good men " +
"to come to the aid of their party.";
string target = "aid";
char[] anyOf = target.ToCharArray();

start = (str.Length-1)/3;
count = (str.Length-1)/4;
Console.WriteLine();
Console.WriteLine("The first character occurrence from position {0} for {1} characters.", start, count);
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str);
Console.Write("A character in '{0}' occurs at position: ", target);
int start = (str.Length - 1) / 3;
int count = (str.Length - 1) / 4;
Console.WriteLine();
Console.WriteLine("The first character occurrence from " +
$"position {start} for {count} characters:");
Console.WriteLine($"{Environment.NewLine}{br1}{Environment.NewLine}{br2}" +
$"{Environment.NewLine}{str}{Environment.NewLine}");
Console.Write($"A character in '{target}' occurs at position: ");

at = str.IndexOfAny(anyOf, start, count);
if (at > -1)
Console.Write(at);
else
Console.Write("(not found)");
Console.WriteLine();
}
}
/*
int at = str.IndexOfAny(anyOf, start, count);
if (at > -1)
Console.Write(at);
else
Console.Write("(not found)");
Console.WriteLine();

/*

The first character occurrence from position 22 for 16 characters.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.
The first character occurrence from position 22 for 16 characters.
0----+----1----+----2----+----3----+----4----+----5----+----6----+-
0123456789012345678901234567890123456789012345678901234567890123456
Now is the time for all good men to come to the aid of their party.

A character in 'aid' occurs at position: 27
A character in 'aid' occurs at position: 27

*/
//</snippet1>
*/
//</snippet1>
}
}
5 changes: 3 additions & 2 deletions snippets/fsharp/System/String/IndexOfAny/IndexOfAny1.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ module IndexOfAny1.fs
let chars = [| 'a'; 'e'; 'i'; 'o'; 'u'; 'y'
'A'; 'E'; 'I'; 'O'; 'U'; 'Y' |]
let s = "The long and winding road..."
printfn $"The first vowel in \n {s}\nis found at position {s.IndexOfAny chars + 1}"
printfn $"The first vowel in \n {s}\nis found at index {s.IndexOfAny chars}"

// The example displays the following output:
// The first vowel in
// The long and winding road...
// is found at position 3
// is found at index 2
// </Snippet1>
13 changes: 7 additions & 6 deletions snippets/visualbasic/System/String/IndexOfAny/IndexOfAny1.vb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
Option Strict On

' <Snippet1>
Module Example
Public Sub Main()
Dim chars() As Char = { "a"c, "e"c, "i"c, "o"c, "u"c, "y"c,
Module Example1
Public Sub Run()
Dim chars() As Char = { "a"c, "e"c, "i"c, "o"c, "u"c, "y"c,
"A"c, "E"c, "I"c, "O"c, "U"c, "Y"c }
Dim s As String = "The long and winding road..."
Console.WriteLine("The first vowel in {2} {0}{2}is found at position {1}",
s, s.IndexOfAny(chars) + 1, vbCrLf)
Console.WriteLine("The first vowel in {2} {0}{2}is found at index {1}",
s, s.IndexOfAny(chars), vbCrLf)
End Sub
End Module

' The example displays the following output:
' The first vowel in
' The long and winding road...
' is found at position 3
' is found at index 2
' </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>

</Project>
8 changes: 4 additions & 4 deletions snippets/visualbasic/System/String/IndexOfAny/ixany2.vb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'<snippet1>
' Sample for String.IndexOfAny(Char[], Int32)
Class Sample
Public Shared Sub Main()
Class Example2
Public Shared Sub Run()
Dim br1 As String = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"
Dim br2 As String = "0123456789012345678901234567890123456789012345678901234567890123456"
Dim str As String = "Now is the time for all good men to come to the aid of their party."
Dim start As Integer
Dim at As Integer
Dim target As String = "is"
Dim anyOf As Char() = target.ToCharArray()

start = str.Length / 2
Console.WriteLine()
Console.WriteLine("Search for a character occurrence from position {0} to {1}.", _
Expand All @@ -34,4 +34,4 @@ End Class
'
'A character in 'is' occurs at position: 49
'
'</snippet1>
'</snippet1>
10 changes: 5 additions & 5 deletions snippets/visualbasic/System/String/IndexOfAny/ixany3.vb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'<snippet1>
' Sample for String.IndexOfAny(Char[], Int32, Int32)
Class Sample
Public Shared Sub Main()
Class Example3
Public Shared Sub Run()
Dim br1 As String = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"
Dim br2 As String = "0123456789012345678901234567890123456789012345678901234567890123456"
Dim str As String = "Now is the time for all good men to come to the aid of their party."
Expand All @@ -10,14 +10,14 @@ Class Sample
Dim count As Integer
Dim target As String = "aid"
Dim anyOf As Char() = target.ToCharArray()

start =(str.Length - 1) / 3
count =(str.Length - 1) / 4
Console.WriteLine()
Console.WriteLine("The first character occurrence from position {0} for {1} characters.", start, count)
Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str)
Console.Write("A character in '{0}' occurs at position: ", target)

at = str.IndexOfAny(anyOf, start, count)
If at > - 1 Then
Console.Write(at)
Expand All @@ -35,4 +35,4 @@ End Class
'
'A character in 'aid' occurs at position: 27
'
'</snippet1>
'</snippet1>
4 changes: 3 additions & 1 deletion xml/System/String.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7846,13 +7846,15 @@ In the following example, the <see cref="M:System.String.IndexOf(System.String,S
<format type="text/markdown"><![CDATA[

## Remarks

Index numbering starts from zero.

The search for `anyOf` is case-sensitive. If `anyOf` is an empty array, the method finds a match at the beginning of the string (that is, at index zero).
The search for `anyOf` is case-sensitive. If `anyOf` is an empty array, the method returns -1.

This method performs an ordinal (culture-insensitive) search, where a character is considered equivalent to another character only if their Unicode scalar values are the same. To perform a culture-sensitive search, use the <xref:System.Globalization.CompareInfo.IndexOf%2A?displayProperty=nameWithType> method, where a Unicode scalar value representing a precomposed character, such as the ligature "Æ" (U+00C6), might be considered equivalent to any occurrence of the character's components in the correct sequence, such as "AE" (U+0041, U+0045), depending on the culture.

## Examples

The following example finds the first vowel in a string.

:::code language="csharp" source="~/snippets/csharp/System/String/IndexOfAny/IndexOfAny1.cs" interactive="try-dotnet" id="Snippet1":::
Expand Down