字符串替换C#中的变音符号[复制](String replace diacritics in C# [duplicate])
这个问题在这里已经有了答案:
我想使用这种方法来创建用户友好的URL。 因为我的网站是克罗地亚语,所以有些字符我不想去掉,但用另一个替换。 例如,这个字符串:
ŠĐĆŽšđčćž
需要是:sdccz-sdccz
所以,我想创建两个数组,其中一个将包含要替换的字符以及其他具有替换字符的数组:string[] character = { "Š", "Đ", "Č", "Ć", "Ž", "š", "đ", "č", "ć", "ž" }; string[] characterReplace = { "s", "d", "c", "c", "z", "s", "d", "c", "c", "z" };
最后,这两个数组应该用在某些方法中,这些方法需要字符串,查找匹配并替换它们。 在PHP中,我使用preg_replace函数来处理这个问题。 在C#中这不起作用:
s = Regex.Replace(s, character, characterReplace);
如果有人能帮忙,我会很感激。 谢谢This question already has an answer here:
I'd like to use this method to create user-friendly URL. Because my site is in Croatian, there are characters that I wouldn't like to strip but replace them with another. Fore example, this string:
ŠĐĆŽ šđčćž
needs to be: sdccz-sdccz
So, I would like to make two arrays, one that will contain characters that are to be replaced and other array with replacement characters:string[] character = { "Š", "Đ", "Č", "Ć", "Ž", "š", "đ", "č", "ć", "ž" }; string[] characterReplace = { "s", "d", "c", "c", "z", "s", "d", "c", "c", "z" };
Finally, this two arrays should be use in some method that will take string, find matches and replace them. In php I used preg_replace function to deal with this. In C# this doesn't work:
s = Regex.Replace(s, character, characterReplace);
Would appreciate if someone could help. Thanks
原文:https://stackoverflow.com/questions/2566793
最满意答案
我认为修改
Array.prototype.sort
是非常危险的(因为可能使用sort()的所有其他代码都会受到影响)。 这是代码://Just use this function like radixsort(homes,0,homes.length,32) function radixsort (arr, begin, end, bit) { var i, j, mask; i = begin; j = end; mask = 1 << bit; while(i < j) { while(i < j && !(arr[i].price & mask)) { ++i; } while(i < j && (arr[j - 1].price & mask)) { --j; } if(i < j) { j--; var tmp = arr[i].price; arr[i].price = arr[j].price; arr[j].price = tmp; i++; } } if(bit && i > begin) { radixsort(arr, begin, i, bit - 1); } if(bit && i < end) { radixsort(arr, i, end, bit - 1); } } //If you really want to modify default sort function: Array.prototype.sort = function(){ radixsort(this,0,this.length,32); //Use 'this' to access (get reference) to the array. }
I think modifying
Array.prototype.sort
is very dangerous (because all other codes probably using sort() will be affected). Here's the code though://Just use this function like radixsort(homes,0,homes.length,32) function radixsort (arr, begin, end, bit) { var i, j, mask; i = begin; j = end; mask = 1 << bit; while(i < j) { while(i < j && !(arr[i].price & mask)) { ++i; } while(i < j && (arr[j - 1].price & mask)) { --j; } if(i < j) { j--; var tmp = arr[i].price; arr[i].price = arr[j].price; arr[j].price = tmp; i++; } } if(bit && i > begin) { radixsort(arr, begin, i, bit - 1); } if(bit && i < end) { radixsort(arr, i, end, bit - 1); } } //If you really want to modify default sort function: Array.prototype.sort = function(){ radixsort(this,0,this.length,32); //Use 'this' to access (get reference) to the array. }
相关问答
更多-
这是一种方法。 它将定义的数组元素复制到一个新数组并保存其索引。 它对新数组进行排序,然后将排序结果放回到以前使用的索引中。 var a = []; a[0] = 3; a[1] = 2; a[2] = 6; a[7] = 4; a[8] = 5; // sortFn is optional array sort callback function, // defaults to numeric sort if not passed function sortSparseArray(arr, s ...
-
主要的问题是,决定是否应该设置参数。 如果不是,则使用myarray进行所有操作。 如果您决定使用参数,则只使用参数变量。 var myarray = [4, 6, 2, 1, 9, ]; document.getElementById("demo").innerHTML = myarray; function sort() { // no need for a parameter var count = mya ...
-
JavaScript [] .sort()方法在应该离开元素时进行排序(JavaScript [ ].sort( ) method sorts when it should leave elements alone)[2022-06-15]
发生这种情况是因为排序不稳定 ,它不一定保留相等项目的相对顺序。 您可以使用稳定的排序算法,如合并排序 。 That happens because the sort is unstable, it doesn't necessarily preserve the relative order of equal items. You can use a stable sorting algorithm, like merge sort. -
如果要忽略大小写,则在排序之前应该小写: console.log('Hello'.toLowerCase().split('').sort().join('')); // ehllo If you want to ignore case, you should lowercase before sorting: console.log('Hello'.toLowerCase().split('').sort().join('')); // ehllo
-
通常适合我: a.sort(function(a,b){ return a - b; }); Usually works for me: a.sort(function(a,b){ return a - b; });
-
我认为修改Array.prototype.sort是非常危险的(因为可能使用sort()的所有其他代码都会受到影响)。 这是代码: //Just use this function like radixsort(homes,0,homes.length,32) function radixsort (arr, begin, end, bit) { var i, j, mask; i = begin; j = end; mask = 1 << bit; while(i ...
-
按降序对数组进行排序而不使用内置方法(sorting an array in descending order javascript without a built-in method)[2022-05-10]
该方法是正确的。 你在代码中只有一个小错误。 取而代之的是: for (var j = i - 1; j >= 0 && (array[j] < array[i]); j--) { 做这个: for (var j = i - 1; j >= 0 && (array[j] < tmp); j--) { 这是必要的,因为在第一次迭代时array[i]的值可能被array[i-1]覆盖,所以在下一次迭代中,你会看到错误的值。 The approach is correct. You just had a ti ... -
是。 如果你在MDN上查看.sort()方法,它会说明省略compare函数参数的情况: 指定定义排序顺序的函数。 如果省略,则根据每个元素的字符串转换,根据每个字符的Unicode代码点值对数组进行排序。 由于Unicode是一组超级ASCII,因此它按ASCII顺序排序。 Yes. If you look at the .sort() method on MDN it states the case when the compare function parameter is omitted: Spec ...
-
javascript数组方法排序返回120大于50 [重复](javascript array method sort returns 120 larger than 50 [duplicate])[2023-12-19]
这是因为它将它们排序为字符串而不是数字。 来自文档 : 如果未提供compareFunction ,则通过将元素转换为字符串并按Unicode代码点顺序比较字符串来对元素进行排序。 例如,“香蕉”出现在“樱桃”之前。 在数字排序中,9出现在80之前,但由于数字被转换为字符串,因此“80”以Unicode顺序出现在“9”之前。 你需要做的是给它一个回调来将它们排序为数字: arr.sort(function(a, b){ return a - b; }); This is because it's ... -
任意重新排序和排序对象文字的javascript数组(Arbitrarily reorder and sort javascript array of object literals)[2022-02-23]
你的措辞有点令人困惑,但这就是我解释它的方式: 您有一个对象数组,每个对象都有一个order属性。 此订单属性必须是唯一的。 您希望将对象A放在一个新位置,然后重新应用订单属性,使它们是唯一且顺序的。 这是我将如何做到这一点: function reorder(object, newIndex) { var oldIndex = toOrder.indexOf(object); toOrder.splice(oldIndex, 1); toOrder.splice(newIndex ...